on_delete

107 views
Skip to first unread message

Rayo Rodriguez

unread,
Sep 30, 2020, 4:03:53 PM9/30/20
to peewee-orm
How to implement on_delete='CASCADE' in peewee, because when I create models, the parameter on_delete is not present, for example: 

products_catalog = ForeignKeyField(column_name='products_catalog_id', field='id', model=ProductsCatalog)

Charles Leifer

unread,
Sep 30, 2020, 5:49:35 PM9/30/20
to peewe...@googlegroups.com
Just specify:

products_catalog = ForeignKeyField(ProductsCatalog, column_name='...', on_delete='cascade')

On Wed, Sep 30, 2020 at 3:03 PM Rayo Rodriguez <rayo.ro...@alphacredit.mx> wrote:
How to implement on_delete='CASCADE' in peewee, because when I create models, the parameter on_delete is not present, for example: 

products_catalog = ForeignKeyField(column_name='products_catalog_id', field='id', model=ProductsCatalog)


Considere el medio ambiente antes de imprimir este mensaje. CONFIDENCIALIDAD. Este mensaje puede contener información confidencial, por lo que su divulgación está prohibida de acuerdo a las leyes aplicables. Si usted no es el destinario o el responsable de la entrega del presente, se le notifica que la publicación, distribución o reproducción de este mensaje, quedan estrictamente prohibidas. AVISO DE PRIVACIDAD. AlphaCredit Capital, S.A. de C.V., SOFOM, E.N.R., con domicilio en Avenida Antonio Dovalí Jaime #70 Torre C, Piso 7, Col. Zedec Santa Fe, C.P. 01210, México D.F., utilizará sus datos personales recabados para proveer y/o realizar gestiones administrativas relacionadas con los servicios y/o productos que ofrece. Para mayor información acerca del tratamiento de los derechos que puede hacer valer, puede acceder al Aviso de Privacidad Integral a través de la página de internet www.alphacredit.mx

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/8941908b-3042-4b29-83df-d20b4be0ef19n%40googlegroups.com.

Rayo Rodriguez

unread,
Sep 30, 2020, 6:01:44 PM9/30/20
to peewee-orm
But then this property is not generated automatic when I generate the models?

Charles Leifer

unread,
Sep 30, 2020, 6:05:20 PM9/30/20
to peewe...@googlegroups.com
Oh, you're auto-generating these from an existing schema? No that is not captured. You will want to specify it. But the value present in the field declaration is only needed when the schema is created -- so since your schema (obviously) already exists, this should be no problem.

Rayo Rodriguez

unread,
Sep 30, 2020, 6:12:29 PM9/30/20
to peewee-orm
But when I use the property delete from peewee, for example:

Table1.delete().where(user==1)

This syntax only deletes the record of that table, but not of the others that depend on it  

Charles Leifer

unread,
Sep 30, 2020, 6:34:09 PM9/30/20
to peewe...@googlegroups.com
What db are you using?

Is your schema pre-existing or created by peewee?

Rayo Rodriguez

unread,
Sep 30, 2020, 6:41:29 PM9/30/20
to peewee-orm
PostgreSQL and the schema is pre-existing 

Charles Leifer

unread,
Sep 30, 2020, 8:41:18 PM9/30/20
to peewe...@googlegroups.com
The peewee `on_delete='cascade'` only applies during schema creation with Peewee. In other words, if your schema already exists, this parameter has no effect. It only exists to tell peewee, when creating the table, to declare the foreign key constraint w/the given ON DELETE setting. Outside of the creation of tables, this setting has no effect.

Since your schema already exists, presumably you created your foreign-key *without* specifying "on delete cascade", which is why the delete isn't cascading? Or alternatively you simply don't have proper foreign-key constraints so Postgres doesn't see any relations it needs to handle.

You can force Peewee to traverse the model graph when deleting an instance by using delete_instance(recursive=True): http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.delete_instance

Reply all
Reply to author
Forward
0 new messages