Re: [sqlalchemy] Not Expiring objects on rollbacks.

2011-07-25 Thread Moch Ramis
2011/7/23 Michael Bayer mike...@zzzcomputing.com


 On Jul 23, 2011, at 5:37 AM, Moch Ramis wrote:

 expunge() will remove any object from the Session


  This look a little dumb, but it seems this will solve my problem:
 expunged objects will not be expired by the session anymore and their data
 will still be accessible don't they? (I'm not certaint about the last
 point)...


 keeping in mind that its been 102 degrees here and I only have a vague
 understanding of your issue, it seems like you're looking to get part of the
 expire-on-rollback behavior, just not for everything.   So if you were to
 use expunge() and expire() directly, you might be able to achieve a similar
 effect.   expunge() would be for those objects that failed to get INSERTed
 and expire() for persistent objects whose state has become stale.

Sorry for that. And yes, after few tests, it seems to work :)




 I understand the problem. I think that letting the objects in the session
 may have been the problem as i did not needed them to be actualized.


 so maybe splitting out these particular INSERTS into ad-hoc
 Session.execute() calls, which don't rollback or do anything when they fail
 except raise.

It may be a cleaner solution. I'll try it.


  --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Cascade Deletes

2011-07-25 Thread Aviv Giladi
I can't seem to make cascade deletes work in sqlalchemy.

I have a parent class (called Rating), a sub class (Subrating) and a
third class called SubRatingProperty.

There is a one-to-one relationship between Rating and SubRating - each
Rating can only have one specific SubRating object. Next, the
SubRatingProperty refers to a row in a table with fixed values. There
are 3 SubRatingProperty entries - property1, property2 and property3.
The SubRating class can have one or more of either property1,
property2 and property3, therefore the relationship is many-to-many (a
SubRatingProperty can have more than one properties, and for example
property1 can be assigned to more than one SubRatingProperty's).

Here is the code that defines all of this:

subrating_subratingproperty_association =
Table('subrating_subratingproperty_association', Base.metadata,
Column('subrating_id', Integer,
ForeignKey('subratings.id')),
Column('subrating_property_id',
Integer, ForeignKey('subrating_properties.id')))

class SubRatingProperty(Base):
__tablename__ = 'subrating_properties'
id = Column(Integer, primary_key=True)
name = Column(Unicode(32), unique=True)
subratings = relationship(SubRating,
 
secondary=subrating_subratingproperty_association,
backref=subrating_properties)

class SubRating(Base):
__tablename__ = 'subratings'
id = Column(Integer, primary_key=True)
name = Column(Unicode(32), unique=True)

class Rating(Base):
__tablename__ = 'ratings'
id = Column(Integer, primary_key=True)
name = Column(Unicode(32), unique=True)
subrating_id = Column(Integer, ForeignKey('subratings.id'))
subrating = relationship(SubRating, backref=backref(rating,
uselist=False))
Everything works fine, but I can't figure out how to do cascade
deletes. I am deleting Rating objects, and when I do, I would like the
according SubRating object to be deleted, as well as all the entries
in the association table. So deleting Rating1 would delete its
SubRating, as well as all the connection between the SubRating and
SubRatingProperty's.

I have tried adding cascade=all to the relationship call, and I also
tried adding ondelete=cascade) to the ForeignKey call. Nothing
seemed to have worked.

How do I set up this cascade deletes business?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.