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
deffilename(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.

    |
    classNode(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
    <http://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 sqlalchemy+...@googlegroups.com <javascript:>.
    To post to this group, send email to sqlal...@googlegroups.com
    <javascript:>.
    Visit this group at http://groups.google.com/group/sqlalchemy
    <http://groups.google.com/group/sqlalchemy>.
    For more options, visit https://groups.google.com/d/optout
    <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 sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to