Migration Order With Foreign Keys

Hello,

I created two migrations with joined tables (the post table having a foreign key towards the author table): create_post_table and create_author_table.

Is the order of these two migrations important ?

If ‘create_post_table’ is executed before ‘create_author_table’, might this lead to an error when Yii tries to create a foreign key towards a table that does not exist ?

Thank you for your clarification,

Fab

Hi joopea

Your database not allows you to insert records with foreign key if not insert first the primary records.

So, you have to insert firstly create_author_table and then create_post_table records

Thank you for your answer,

It would be nice if Yii could automatically re-order the migrations and avoid such dependency problems.

When working on big databases, it quickly becomes tricky to find manually the right migration order.

By the way, I tried to rename my migration files/classes to change their order (m130625_120317_create_author_table renamed to m130625_01_create_author_table) but I get the following message:


No new migrations found. Your system is up-to-date.

… while my database is empty :(

Could you please help me ?

Fab

Hi yoopea,

You could consider creating a single migration which creates author table and post table, because we could think of them as a unit that can’t be divided.

Or, you would rather want them to be 3 steps: creating post table, creating author table, and adding foreign key constraint to post table.

As for renaming:

"m130625_01_…" is lesser than "m130626_120317…". That means, the former is the older.

And check "tbl_migration" table in your db. Yii records the applied migrations in that table. I guess the latest applied migration should be newer than "m130625_01_…" in it.