On Wed, Oct 19, 2011 at 7:57 PM, Michael Bayer <[email protected]>wrote:
>
> > but I can't add options contains_eager like
> > result = result.options(contains_eager(Comment.user))
>
> Well no because you're digging way into RECURSIVE queries which SQLA
> doesn't yet support very nicely. Mapping to them is not a problem that's
> been cleanly solved.
>
>
Without recursive can I use non_primary mapper with contains_eager for
creating model instances?
like this:
class Test(db.Model):
__tablename__ = 'test'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User', backref=db.backref('test_user',
cascade='all, delete, delete-orphan', lazy='dynamic'))
reply_id = db.Column(db.Integer, db.ForeignKey('test.id'))
children = db.relationship('Test', backref=db.backref('parent',
remote_side=id))
text = db.Column(db.Text, nullable=False)
from sqlalchemy.orm import mapper
non_primary_mapper = mapper(Test, Test.__table__, non_primary=True)
result1 = db.session.query(Test).from_statement('SELECT test.id AS test_id,
test.user_id AS test_user_id, test.reply_id AS test_reply_id, test.text AS
test_text FROM test LEFT OUTER JOIN "user" ON "user".id = test.user_id LIMIT
1 OFFSET 0').options(contains_eager(Test.user)).all()
Ok, it works, but:
result2 = db.session.query(non_primary_mapper).from_statement('SELECT
test.id AS test_id, test.user_id AS test_user_id, test.reply_id AS
test_reply_id, test.text AS test_text FROM test LEFT OUTER JOIN "user" ON
"user".id = test.user_id LIMIT 1 OFFSET
0').options(contains_eager(Test.user)).all()
raise:
ArgumentError: Can't find property 'user' on any entity specified in this
Query. Note the full path from root (Mapper|Test|test|non-primary) to target
entity must be specified.
Can any way to solve this?
Thanks.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.