Thanks a lot! It works but I still have a problem. I want to make one query
to get the root directory, all its children and the filenames of Image
objects.
My directory model looks like this:
class Directory(Node):
is_root = db.Column(db.Boolean, default=False)
children = db.relationship(Node, cascade_backrefs=False, backref=db.
backref('parent', remote_side=[Node.id]), lazy='joined', join_depth=10)
When I use the query that you suggested I get the filenames instantly but I
don't get the eager loading of the children. I also tried this:
db.session.query(Node).with_polymorphic([Directory, Image]).all()
And I still only get the filenames without eager loading of Directory
children.
W dniu wtorek, 15 września 2015 17:17:12 UTC+2 użytkownik Michael Bayer
napisał:
>
>
>
> On 9/15/15 10:09 AM, Jakub Bąk wrote:
>
> I used before_delete and after_commit and it works like a charm. Thanks
> again!
>
> I have another problem related to this setup. I have a property defined on
> the Image model:
>
> @property
> def filename(self):
> return '{basename}.{extension}'.format(basename=self.basename,
> extension=self.extension)
>
> When I make this query:
>
> image = db.session.query(Image).get(100)
> image.filename
>
> I get the filename property istantly but when I do it like this:
>
> node = db.session.query(Node).get(100)
> node.filename
>
> Another query has to be issued which is not what I want. Do you know any
> possible solution to that problem Michael?
>
>
> that is again the way the joined-table inheritance works; by default it
> only queries the base table. Per query you'd do it like this:
>
> session.query(Node).with_polymorphic(Image).get(100)
>
>
>
>
>
>
> W dniu wtorek, 25 sierpnia 2015 17:16:16 UTC+2 użytkownik Michael Bayer
> napisał:
>>
>>
>>
>> On 8/25/15 10:05 AM, Jakub Bąk wrote:
>>
>> I just came across a solution to this problem. Adding 'with_polymorphic':
>> '*' to __mapper_args__ on the Node model was enough.
>>
>> class Node(db.Model):
>> id = db.Column(db.Integer, primary_key=True)
>> type = db.Column(db.String(20))
>> name = db.Column(db.String(30), nullable=False)
>> date_added = db.Column(db.DateTime, default=datetime.now())
>> parent_id = db.Column(db.Integer, db.ForeignKey('node.id'), nullable=
>> True)
>> path = db.Column(db.String(200), default='')
>> __mapper_args__ = {'polymorphic_on': type,
>> 'with_polymorphic': '*' }
>>
>>
>> OK, yeah that's a funny problem you have. The with_polymorphic in all
>> cases is going to lead to heavier queries, because there's the join but
>> also if you already have joins between Node classes, you'll start to see
>> some very long queries with lots of joins / sub-joins.
>>
>> A more portable solution would be to also implement before_delete(),
>> where you either ensure that the Image columns are loaded, or you add the
>> files to be deleted to some kind of collection; then you'd actually delete
>> the files using a safer event like after_commit(), so that way if your
>> transaction fails, the files are preserved.
>>
>>
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sqlalchemy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.