On Wed, Oct 9, 2019, at 5:14 AM, 'Marc Vegetti' via sqlalchemy wrote: > Hello, > > I'm currently auditing a database which uses sqlalchemy 1.1.9 and PostgreSQL > 10 with Python 3.5. > > There are a few foreign keys which are set to *ON DELETE CASCADE* on the > database but are not set as such on the ORM side. > My first thought was to simply add in the ForeignKey, a *ondelete='CASCADE'* > but then I saw this article: > https://docs.sqlalchemy.org/en/13/orm/cascades.html - ORM-level “delete” > cascade vs. FOREIGN KEY level “ON DELETE” cascade. > > What I don't understand is, why would I want the ORM to handle the cascade if > it explicitly says : >> Database level `ON DELETE` cascade is *vastly more efficient* than that of >> SQLAlchemy.
because "ON DELETE CASCADE" is not supported by all databases or database configurations. There's also the case where the primaryjoin condition of a relationship is against columns that don't have a real foreign key constraint on them. > Furthermore how sqlachemy cascade handles collisions with the database > cascade if the first one uses a one-to-many schema while the other one uses a > many-to-one ? I need more detail to understand what that question is asking re: one-to-many while "the other one" uses many-to-one, what is"the other one" ? > > And finally, should I follow my first idea and simply add the > *ondelete='CASCADE'* and expect the same behavior as before ? if your relationships match your schema appropriately then yes, you should have unit tests in place which confirm it does what you want. > > Best regards, > > Marc V. > > -- > 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 sqlalchemy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/02b34afa-4f4e-46bb-9c46-6012e19d25f8%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/02b34afa-4f4e-46bb-9c46-6012e19d25f8%40googlegroups.com?utm_medium=email&utm_source=footer>. -- 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 sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/ab62f1ab-6d82-4676-8d48-3202df9f7233%40www.fastmail.com.