Add ondelete='CASCADE' to existing foreignkey

823 views
Skip to first unread message

Hồng Quân Nguyễn

unread,
Dec 15, 2013, 11:07:39 AM12/15/13
to sqlalchem...@googlegroups.com
Hi

I defined a model with a column as foreignkey, but forgot to include "ondelete='CASCADE'". Now I want to add this, how should I do? (alembic revision --auto doesn't support)

Thanks.

Michael Bayer

unread,
Dec 15, 2013, 11:31:14 AM12/15/13
to sqlalchem...@googlegroups.com
i believe you need to drop the foreign key and re-create it.


--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

signature.asc

Nguyễn Hồng Quân

unread,
Dec 16, 2013, 12:52:35 AM12/16/13
to sqlalchem...@googlegroups.com
Hi Michael,

Could you please tell me how the function, syntax is.

In the Alembic documentation, I can see create_foreign_key but not drop_foreign_key. I guess drop_constraint can be used, but please give me an example.
Another thing is that the foreign_key generated by
Column('owner_id', Integer, ForeignKey('owners.id'))
doesn't have a name, but drop_constraint require the constraint's name to drop.


2013/12/16 Michael Bayer <mik...@zzzcomputing.com>

i believe you need to drop the foreign key and re-create it.

--
***********************************************
* Nguyễn Hồng Quân                            *
* Y!M: ng_hquan_vn                            *
* Identi.ca: hongquan                         *
***********************************************

Michael Bayer

unread,
Dec 16, 2013, 10:37:52 AM12/16/13
to sqlalchem...@googlegroups.com
On Dec 16, 2013, at 12:52 AM, Nguyễn Hồng Quân <ng.hon...@gmail.com> wrote:

Hi Michael,

Could you please tell me how the function, syntax is.

it’s drop_constraint:

op.drop_constraint(“name_of_fk”)


Another thing is that the foreign_key generated by
Column('owner_id', Integer, ForeignKey('owners.id'))
doesn't have a name, but drop_constraint require the constraint's name to drop.

the database always gives it a name, you can see it with a script like this:

from sqlalchemy import create_engine, inspect

eng = create_engine(“…”)
insp = inspect(eng)

print(insp.get_foreign_keys(“my_table”))


signature.asc
Reply all
Reply to author
Forward
0 new messages