Discussion:
Add ondelete='CASCADE' to existing foreignkey
Hồng Quân Nguyễn
2013-12-15 16:07:39 UTC
Permalink
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)
Michael Bayer
2013-12-15 16:31:14 UTC
Permalink
i believe you need to drop the foreign key and re-create it.
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.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
For more options, visit https://groups.google.com/groups/opt_out.
Nguyễn Hồng Quân
2013-12-16 05:52:35 UTC
Permalink
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.
Post by Michael Bayer
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 *
***********************************************
--
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-alembic+***@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-12-16 15:37:52 UTC
Permalink
Post by Nguyễn Hồng Quân
Hi Michael,
Could you please tell me how the function, syntax is.
it’s drop_constraint:

op.drop_constraint(“name_of_fk”)
Post by Nguyễn Hồng Quân
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”))

Loading...