phalcon / migrations

Generate or migrate database changes via migrations.
https://docs.phalcon.io/latest/en/db-migrations
BSD 3-Clause "New" or "Revised" License
27 stars 23 forks source link

[NFR] Drop foreign keys before column drop #6

Open Jurigag opened 8 years ago

Jurigag commented 8 years ago

Let's assume that we have in current database some foreign key on location.instrument_id. Lets assume that we are deleting this column. This won't work because there is existing foriegn key.

ERROR: SQLSTATE[HY000]: General error: 1828 Cannot drop c
olumn 'instrument_id': needed in a foreign key constraint 'suzuki_test/location_ibfk_2'

Phalcon migration should check if there is any foreign key like:

select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = :currentTable: AND COLUMN_NAME = :currentColumn: AND REFERENCED_COLUMN_NAME IS NOT NULL;

Then on result from it it should delete all constraint with CONSTRAINT_NAME.

sergeysviridenko commented 7 years ago

@Jurigag I'm not sure that deleting is good way. FK is may cause problems when during on generating or running migration. But we can implement workflow with FK to store keys in the file. For example: user runs command generate --FK and DevTools will create file called foreignKeys.sql with real SQL. After that we can delete it from DB. I guess this way is better. How do you think about this?