On Aug 19, 2010, at 10:22 AM, sandro dentella wrote: > Hi, > > Let's start with the simple example based on movie/director example > > class Director(Base): > __tablename__ = 'director' > id = Column(Integer, primary_key=True) > last_name = Column(String(60), nullable=False) > movies = relation('Movie', backref='director', > cascade='all, delete-orphan',) > > class Movie(Base): > __tablename__ = 'movie' > id = Column(Integer, primary_key=True) > title = Column(String(60), nullable=False) > director_id = Column(Integer, ForeignKey('director.id'), > nullable=False) > > Due to delete-orphan cascading rule when creating a movie object I > need to > attach a director object. In a table editor when setting a foreign key > I > parse all the properties to see if someone requires such 'director' > obj so > that I can easily fetch it and attach. The working code in 0.5 is as > follows: > > def get_props_for_delete_orphan(mapper, field_name): > """ > discover if field_name setting needs to set an object to > fullfill the request > for a delete_orphan cascading on a possible relation > returns the generator for the properties or () > """ > prop = mapper.get_property(field_name) > assert isinstance(prop, properties.ColumnProperty) > assert len(prop.columns) == 1 # I don't handle mapper with two > columns in a property > column = prop.columns[0] > > props = [] > for pr in mapper.iterate_properties: > if isinstance(pr, properties.RelationProperty): > if pr.direction.name in ('MANYTOONE',): > for col in pr.local_remote_pairs[0]: > # I can't use col in p.local_remote_pairs > # as it uses 'col == p.local_remote_pairs' that evaluates > # to a BinaryExpression > if column is col: > try: > if pr.backref.prop.cascade.delete_orphan: > props += [pr] > except AttributeError, e: > pass > return tuple(props) > > This fails in sqla 0.6 as pr.backref is empty. Which is the correct > way to > get the properties that have a backref that have cascade with > delete_orphan?
why not put some "info" on the director_id column (i.e. Column(...., info={'foo':'bar'}) ) that gives your application the information what you need in a succinct and direct way. There's no public API that links relationships to backrefs and the poking through lists of columns is hacky too. I could tell you where they're linked but it can change at any time. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.