Get sqlalchemy base class object instead of children
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have three classes in my model, which one class inherited by the other two:
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
title = Column(Unicode(300))
type = Column(Unicode(50))
__mapper_args__ = {
'polymorphic_on': type
}
class Note(Item):
__tablename__ = 'note'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'note'
}
class Task(Item):
__tablename__ = 'task'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
another_extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'task'
}
So, when I execute session.query(Item).all()
I get a list that includes both Note
and Task
objects, but I don't want that, I want my objects to be the instance of Item
class and just have id
, title
, type
, not those extra fields. how should I write the query?
to clarify more, currently, I get:
[
<models.Note object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
...
]
But I want to get:
[
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
...
]
python python-3.x inheritance sqlalchemy
|
show 3 more comments
I have three classes in my model, which one class inherited by the other two:
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
title = Column(Unicode(300))
type = Column(Unicode(50))
__mapper_args__ = {
'polymorphic_on': type
}
class Note(Item):
__tablename__ = 'note'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'note'
}
class Task(Item):
__tablename__ = 'task'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
another_extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'task'
}
So, when I execute session.query(Item).all()
I get a list that includes both Note
and Task
objects, but I don't want that, I want my objects to be the instance of Item
class and just have id
, title
, type
, not those extra fields. how should I write the query?
to clarify more, currently, I get:
[
<models.Note object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
...
]
But I want to get:
[
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
...
]
python python-3.x inheritance sqlalchemy
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive anItem
type back?
– SuperShoot
Nov 24 '18 at 12:57
@SuperShoot I need them to be an instance ofItem
class. Thanks for the like by the way.
– Mehrdad Pedramfar
Nov 24 '18 at 12:59
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I thinkjust_item = with_polymorphic(Item, [Note, Task])
then followed by:session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html
– Hans Bouwmeester
Nov 24 '18 at 16:38
@HansBouwmeesterwith_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
andTask
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still beNote
andTask
objects.
– SuperShoot
Nov 24 '18 at 21:59
|
show 3 more comments
I have three classes in my model, which one class inherited by the other two:
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
title = Column(Unicode(300))
type = Column(Unicode(50))
__mapper_args__ = {
'polymorphic_on': type
}
class Note(Item):
__tablename__ = 'note'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'note'
}
class Task(Item):
__tablename__ = 'task'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
another_extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'task'
}
So, when I execute session.query(Item).all()
I get a list that includes both Note
and Task
objects, but I don't want that, I want my objects to be the instance of Item
class and just have id
, title
, type
, not those extra fields. how should I write the query?
to clarify more, currently, I get:
[
<models.Note object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
...
]
But I want to get:
[
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
...
]
python python-3.x inheritance sqlalchemy
I have three classes in my model, which one class inherited by the other two:
class Item(Base):
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
title = Column(Unicode(300))
type = Column(Unicode(50))
__mapper_args__ = {
'polymorphic_on': type
}
class Note(Item):
__tablename__ = 'note'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'note'
}
class Task(Item):
__tablename__ = 'task'
id = Column(Integer, ForeignKey('item.id'), primary_key=True)
another_extra = Column(Text)
__mapper_args__ = {
'polymorphic_identity': 'task'
}
So, when I execute session.query(Item).all()
I get a list that includes both Note
and Task
objects, but I don't want that, I want my objects to be the instance of Item
class and just have id
, title
, type
, not those extra fields. how should I write the query?
to clarify more, currently, I get:
[
<models.Note object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
<models.Task object at 0x7f25ac3ffe80>,
...
]
But I want to get:
[
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
<models.Item object at 0x000000000000>,
...
]
python python-3.x inheritance sqlalchemy
python python-3.x inheritance sqlalchemy
edited Nov 26 '18 at 5:53
Mehrdad Pedramfar
asked Nov 24 '18 at 12:10
Mehrdad PedramfarMehrdad Pedramfar
6,55311644
6,55311644
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive anItem
type back?
– SuperShoot
Nov 24 '18 at 12:57
@SuperShoot I need them to be an instance ofItem
class. Thanks for the like by the way.
– Mehrdad Pedramfar
Nov 24 '18 at 12:59
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I thinkjust_item = with_polymorphic(Item, [Note, Task])
then followed by:session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html
– Hans Bouwmeester
Nov 24 '18 at 16:38
@HansBouwmeesterwith_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
andTask
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still beNote
andTask
objects.
– SuperShoot
Nov 24 '18 at 21:59
|
show 3 more comments
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive anItem
type back?
– SuperShoot
Nov 24 '18 at 12:57
@SuperShoot I need them to be an instance ofItem
class. Thanks for the like by the way.
– Mehrdad Pedramfar
Nov 24 '18 at 12:59
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I thinkjust_item = with_polymorphic(Item, [Note, Task])
then followed by:session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html
– Hans Bouwmeester
Nov 24 '18 at 16:38
@HansBouwmeesterwith_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
andTask
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still beNote
andTask
objects.
– SuperShoot
Nov 24 '18 at 21:59
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive an
Item
type back?– SuperShoot
Nov 24 '18 at 12:57
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive an
Item
type back?– SuperShoot
Nov 24 '18 at 12:57
@SuperShoot I need them to be an instance of
Item
class. Thanks for the like by the way.– Mehrdad Pedramfar
Nov 24 '18 at 12:59
@SuperShoot I need them to be an instance of
Item
class. Thanks for the like by the way.– Mehrdad Pedramfar
Nov 24 '18 at 12:59
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I think
just_item = with_polymorphic(Item, [Note, Task])
then followed by: session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html– Hans Bouwmeester
Nov 24 '18 at 16:38
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I think
just_item = with_polymorphic(Item, [Note, Task])
then followed by: session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html– Hans Bouwmeester
Nov 24 '18 at 16:38
@HansBouwmeester
with_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
and Task
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still be Note
and Task
objects.– SuperShoot
Nov 24 '18 at 21:59
@HansBouwmeester
with_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
and Task
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still be Note
and Task
objects.– SuperShoot
Nov 24 '18 at 21:59
|
show 3 more comments
1 Answer
1
active
oldest
votes
NOTE: This may be problematic in a multi-threaded application.
You could use a context manager to temporarily block the polymorphism:
from contextlib import contextmanager
from sqlalchemy import inspect
@contextmanager
def no_poly(class_):
mapper = inspect(class_).mapper
polycol = mapper.polymorphic_on
mapper.polymorphic_on = None
yield class_
mapper.polymorphic_on = polycol
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
task = Task(title='Task Title', another_extra='something')
s = Session()
s.add(task)
s.commit()
# opening a new session as if the pk already exists in the
# identity map it will return whatever type that pk is
# pointing at.
s = Session()
with no_poly(Item) as class_:
inst = s.query(class_).all()
print(inst) # [<__main__.Item object at 0x000001443685DDD8>]
s = Session() # new session again.
inst = s.query(Item).all()
print(inst) # [<__main__.Task object at 0x00000144368770B8>]
Something to be mindful of and as noted in the comments in my example, if the identity of the object is already referenced in the Identity Map, then you will get back whatever type is held in there, regardless of the class that you query on.
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modifymapper
, I was just looking for a query or__mapper_args__
to work somehow.
– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458005%2fget-sqlalchemy-base-class-object-instead-of-children%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
NOTE: This may be problematic in a multi-threaded application.
You could use a context manager to temporarily block the polymorphism:
from contextlib import contextmanager
from sqlalchemy import inspect
@contextmanager
def no_poly(class_):
mapper = inspect(class_).mapper
polycol = mapper.polymorphic_on
mapper.polymorphic_on = None
yield class_
mapper.polymorphic_on = polycol
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
task = Task(title='Task Title', another_extra='something')
s = Session()
s.add(task)
s.commit()
# opening a new session as if the pk already exists in the
# identity map it will return whatever type that pk is
# pointing at.
s = Session()
with no_poly(Item) as class_:
inst = s.query(class_).all()
print(inst) # [<__main__.Item object at 0x000001443685DDD8>]
s = Session() # new session again.
inst = s.query(Item).all()
print(inst) # [<__main__.Task object at 0x00000144368770B8>]
Something to be mindful of and as noted in the comments in my example, if the identity of the object is already referenced in the Identity Map, then you will get back whatever type is held in there, regardless of the class that you query on.
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modifymapper
, I was just looking for a query or__mapper_args__
to work somehow.
– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
add a comment |
NOTE: This may be problematic in a multi-threaded application.
You could use a context manager to temporarily block the polymorphism:
from contextlib import contextmanager
from sqlalchemy import inspect
@contextmanager
def no_poly(class_):
mapper = inspect(class_).mapper
polycol = mapper.polymorphic_on
mapper.polymorphic_on = None
yield class_
mapper.polymorphic_on = polycol
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
task = Task(title='Task Title', another_extra='something')
s = Session()
s.add(task)
s.commit()
# opening a new session as if the pk already exists in the
# identity map it will return whatever type that pk is
# pointing at.
s = Session()
with no_poly(Item) as class_:
inst = s.query(class_).all()
print(inst) # [<__main__.Item object at 0x000001443685DDD8>]
s = Session() # new session again.
inst = s.query(Item).all()
print(inst) # [<__main__.Task object at 0x00000144368770B8>]
Something to be mindful of and as noted in the comments in my example, if the identity of the object is already referenced in the Identity Map, then you will get back whatever type is held in there, regardless of the class that you query on.
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modifymapper
, I was just looking for a query or__mapper_args__
to work somehow.
– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
add a comment |
NOTE: This may be problematic in a multi-threaded application.
You could use a context manager to temporarily block the polymorphism:
from contextlib import contextmanager
from sqlalchemy import inspect
@contextmanager
def no_poly(class_):
mapper = inspect(class_).mapper
polycol = mapper.polymorphic_on
mapper.polymorphic_on = None
yield class_
mapper.polymorphic_on = polycol
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
task = Task(title='Task Title', another_extra='something')
s = Session()
s.add(task)
s.commit()
# opening a new session as if the pk already exists in the
# identity map it will return whatever type that pk is
# pointing at.
s = Session()
with no_poly(Item) as class_:
inst = s.query(class_).all()
print(inst) # [<__main__.Item object at 0x000001443685DDD8>]
s = Session() # new session again.
inst = s.query(Item).all()
print(inst) # [<__main__.Task object at 0x00000144368770B8>]
Something to be mindful of and as noted in the comments in my example, if the identity of the object is already referenced in the Identity Map, then you will get back whatever type is held in there, regardless of the class that you query on.
NOTE: This may be problematic in a multi-threaded application.
You could use a context manager to temporarily block the polymorphism:
from contextlib import contextmanager
from sqlalchemy import inspect
@contextmanager
def no_poly(class_):
mapper = inspect(class_).mapper
polycol = mapper.polymorphic_on
mapper.polymorphic_on = None
yield class_
mapper.polymorphic_on = polycol
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
task = Task(title='Task Title', another_extra='something')
s = Session()
s.add(task)
s.commit()
# opening a new session as if the pk already exists in the
# identity map it will return whatever type that pk is
# pointing at.
s = Session()
with no_poly(Item) as class_:
inst = s.query(class_).all()
print(inst) # [<__main__.Item object at 0x000001443685DDD8>]
s = Session() # new session again.
inst = s.query(Item).all()
print(inst) # [<__main__.Task object at 0x00000144368770B8>]
Something to be mindful of and as noted in the comments in my example, if the identity of the object is already referenced in the Identity Map, then you will get back whatever type is held in there, regardless of the class that you query on.
edited Nov 26 '18 at 6:30
answered Nov 25 '18 at 2:38
SuperShootSuperShoot
2,3231024
2,3231024
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modifymapper
, I was just looking for a query or__mapper_args__
to work somehow.
– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
add a comment |
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modifymapper
, I was just looking for a query or__mapper_args__
to work somehow.
– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
3
3
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
This seems like it might pose some risks in multithreaded programs, since it modifies the mapper in place.
– Ilja Everilä
Nov 26 '18 at 5:48
IIja is correct, I don't really want to modify
mapper
, I was just looking for a query or __mapper_args__
to work somehow.– Mehrdad Pedramfar
Nov 26 '18 at 6:01
IIja is correct, I don't really want to modify
mapper
, I was just looking for a query or __mapper_args__
to work somehow.– Mehrdad Pedramfar
Nov 26 '18 at 6:01
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
@IljaEverilä do you have any idea about how to achive this?
– Mehrdad Pedramfar
Nov 26 '18 at 6:02
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
Yeh, it's not threadsafe. I'll note that in the answer. Is that a prerequisite in this case?
– SuperShoot
Nov 26 '18 at 6:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458005%2fget-sqlalchemy-base-class-object-instead-of-children%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Would it be enough to just query for those specific columns and get a named tuple back? Or do you absolutely need to receive an
Item
type back?– SuperShoot
Nov 24 '18 at 12:57
@SuperShoot I need them to be an instance of
Item
class. Thanks for the like by the way.– Mehrdad Pedramfar
Nov 24 '18 at 12:59
Not sure if it will help you, but I noticed that if you remove the polymorphic identities it works as you expect.
– Hans Bouwmeester
Nov 24 '18 at 16:07
The code doesn't run for me (getting: "No such polymorphic_identity 'type' is defined")(?) but per the docs I think
just_item = with_polymorphic(Item, [Note, Task])
then followed by:session.query(just_item).all()
may work? See: docs.sqlalchemy.org/en/latest/orm/inheritance_loading.html– Hans Bouwmeester
Nov 24 '18 at 16:38
@HansBouwmeester
with_polymorphic(Item, [Note, Task])
ensures that when querying on the base class, the attributes belonging to the the list of classes (Note
andTask
in this example) are eager loaded so that the resultant objects will not have to lazy load attribute values later. The objects returned from that query will still beNote
andTask
objects.– SuperShoot
Nov 24 '18 at 21:59