Thank you Michael, now I understand :)
My model must contains this parametr:
class Person(Base):
...
group_leader_id = Column(Integer, ForeignKey('person.id',
onupdate="CASCADE", ondelete="CASCADE"), nullable=True)
group_leader = relationship("Person", remote_side=[id],
primaryjoin="Person.id==Person.group_leader_id")
addressess_id = Column(Integer, ForeignKey('addressess.id',
onupdate="CASCADE", ondelete="CASCADE"), nullable=True)
addressess = relationship("Addressess", backref=backref("person",
uselist=False))
price_id = Column(Integer, ForeignKey('prices.id', onupdate="CASCADE",
ondelete="CASCADE"), nullable=True)
price = relationship("Price", backref=backref("person", uselist=False))
class Group(Base):
...
leader_id = Column(Integer, ForeignKey('person.id', onupdate="SET
NULL", ondelete="SET NULL"))
leader = relationship("Person")
addressess_id = Column(Integer, ForeignKey('addressess.id',
onupdate="SET NULL", ondelete="SET NULL"))
addressess = relationship("Addressess")
association_table = Table('association', Base.metadata,
Column('person_id', Integer, ForeignKey('person.id',
onupdate="CASCADE", ondelete="CASCADE")),
Column('projects_id', Integer, ForeignKey('projects.id',
onupdate="CASCADE", ondelete="CASCADE"))
)
class Projects(Base):
...
supervisor_id = Column(Integer, ForeignKey('person.id',
onupdate="CASCADE", ondelete="CASCADE"))
*But what I get in exchange for setting
cascade?*(http://docs.sqlalchemy.org/en/latest/orm/session.html#cascades)
class Order(Base):
__tablename__ = 'order'
items = relationship("Item", cascade="all, delete-orphan")
*I do not understand the documentation.*
W dniu sobota, 6 lipca 2013 16:53:10 UTC+2 użytkownik Michael Bayer napisał:
>
>
> On Jul 6, 2013, at 4:39 AM, MacVictor <[email protected] <javascript:>>
> wrote:
>
>
>
> *When I try used example query delete, always show error:*
>
> session.query(Person).filter(Person.email.like('%@email.com
> ')).delete(synchronize_session='fetch')
>
> subq = session.query(Person.id).filter(Person.name.like('%P%')).subquery()
>
> session.query(Person).filter(Person.group_leader_id.in_(subq)).delete(synchronize_session='fetch')
>
> subq =
> session.query(Addressess.id).filter(Addressess.street.like('%Jana%')).subquery()
>
> session.query(Projects).filter(Projects.students.any(Person.addressess_id.in_(subq))).delete(synchronize_session='fetch')
>
> subq =
> session.query(Addressess.id).filter(Addressess.postal_code.like('%01%')).subquery()
>
> session.query(Person).filter(Person.addressess_id.in_(subq)).delete(synchronize_session='fetch')
>
> session.query(Person).delete(synchronize_session='fetch')
>
>
> *this error:*
>
> (IntegrityError) update or delete on table "person" violates foreign key
> constraint "association_person_id_fkey" on table "association"
> DETAIL: Key (id)=(2990) is still referenced from table "association".
> 'DELETE FROM person WHERE person.email LIKE %(email_1)s' {'email_1': '%@
> email.com%'}
>
>
> *I read docs:
> http://docs.sqlalchemy.org/en/latest/orm/session.html#cascades but I do
> not understand how to set cascade deleting.*
> *I using self ForeignKey and Many2Many filter.. how to set these fields?*
>
>
> SQLAlchemy's ORM-level "cascade" that you set on relationship() doesn't
> apply to using query().delete(). query().delete() just emits a DELETE
> statement to the database directly without taking into account what is
> dependent on it and such. You can get the database itself to take care of
> dependent rows if you configure your actual schema with appropriate "ON
> DELETE CASCADE" instructions (read about it here:
> http://www.postgresql.org/docs/8.2/static/ddl-constraints.html or google
> it). You can configure that from ForeignKey() using "ondelete", see
> http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html#on-update-and-on-delete
>
>
>
--
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/groups/opt_out.