On Jul 6, 2013, at 4:39 AM, MacVictor <[email protected]> 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.