On Fri, Jul 21, 2017 at 7:57 AM, Mauro Caceres <[email protected]> wrote:
> Hi Guys,
>
> Im using sqlachemy SQLAlchemy==1.0.14 on PostgresDB.
>
> Im trying to understand if it is possible to delete all childs in a one to
> many relation but with the distinction that there would be orphans in the
> table, that is childs with the foreign key in NULL.
>
> This is what I have....
>
> Class Parent(Base):
> __tablename__ = 'parent_table'
> id = Column(Integer, primary_key=True)
>
>
> Class Child(Base):
> __tablename__ = 'parent_table'
> id = Column(Integer, primary_key=True)
> parent_id = Column(Integer, ForeignKey('parent_table.id',
> ondelete='CASCADE'), index=True, nulleable=True)
> parent = relationship("Parent", backref=backref("childs",
> passive_deletes=True,
> cascade="all, delete"))
>
>
> With the above definition when I do:
>
> parent = s.query(Parent).first()
> s.delete(parent)
>
> The parent row is deleted, but the related childs are not, the cascade
> relation does not work.
>
> If I remove the nulleable=True from the foreingkey definition then the
> cascade works as expected.... Is any configuration that I may be missing to
> make it work with the nulleable=True...
Hi there -
let's work with a complete test:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parent_table'
id = Column(Integer, primary_key=True)
class Child(Base):
__tablename__ = 'child_table'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent_table.id',
ondelete='CASCADE'),
index=True, nullable=True)
parent = relationship("Parent", backref=backref("childs",
passive_deletes=True,
cascade="all, delete"))
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)
s = Session(e)
s.add(Parent(childs=[Child(), Child()]))
s.commit()
parent = s.query(Parent).first()
s.delete(parent)
s.commit()
# child objects were deleletd
assert s.query(Child).count() == 0
the above test succeeds for me. Does it work for you?
>
> Thanks a lot.
>
>
> This email and any files transmitted with it are confidential and intended
> solely for the use of the individual or entity to whom they are addressed.
> If you have received this email in error please notify the system manager.
> This message contains confidential information and is intended only for the
> individual named. If you are not the named addressee you should not
> disseminate, distribute or copy this e-mail.
> Please notify the sender immediately by e-mail if you have received this
> e-mail by mistake and delete this e-mail from your system. If you are not
> the intended recipient you are notified that disclosing, copying,
> distributing or taking any action in reliance on the contents of this
> information is strictly prohibited.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.