-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better constraints management in migrations #1
Comments
Automatic generation can be applied for example if the constraint name is skipped, so user can choose between manual / automatical handling of it. Example with $this->addForeignKey(
null,
'messages',
'user_id',
'users',
'id',
'CASCADE',
'CASCADE'
); Generated foreign key name will be for example: Dropping: $this->dropForeignKey(null, 'messages', ['id' => 'users.id']); We can retrieve this string |
Seems like this concept already used in Phinx. Not sure about their implementation of it. |
And in Phinx relations are not even specified for dropping foreign keys, column name is enough. |
Also similar concept is used in Laravel (for dropping you need to specify string though). |
I like the idea. I'm using PostgreSQL and it builds constraint names from it's definition, like table name and column names for FK and index constraints, adding Other DBMS need to be checked about this. |
👍 |
Absolutely aggree, currently it is a pain to write foreight keys every time |
The issue is that each DBMS names its keys differently so I would not rely on it. Personally I'm using |
What worries me is that there could be an index for multiple columns and there could be multiple indexes of different types for a single column. |
Many foreign keys for a single column happens daily. |
OK, so it's better to use uniform naming convention instead. But as @nineinchnick said, DBMS need to be checked for max length limitations etc. |
@samdark Could you provide an example of multiple foreign keys? |
That's morning sleepy typo. Of course, I've meant indexes. |
you could overide it yourself for this particular case and let it auto generate. at the end off the UP i add all the fk's with auto generated names. at the beginning of the "down" i remove the keys by the using the same configuration. thus the same way its build. |
Of course we can override and implement by ourselves, but I personally expect this feature out of the box. |
the problem with that is that project that move to Yii with an existing database cant use that feature. |
Some kind of migration mechanism can be implemented. Alternatively users can continue to use existing algorithm (I described it above). |
#5 seems the issue was incorrectly split when moving. |
Currently we have to specify foreign key name with both adding and dropping.
Some DBMS like MySQL generate it automatically, but the dropping part for me is more frustrating.
You have to look through all previous migrations and find the name of corresponding foreign key.
One solution will be following some convention and ask team members to follow it too, but it will be great if:
And foreign key will be found and dropped automatically.
That way developer only cares about relations and not names.
I think It can be applied to primary keys, indices, etc. too.
What do you think? Is it possible with DBMS?
Maybe use some naming convention if DBMS don't support auto generation? In this case we can build constraint name string depending on relations and add / drop it.
Funding
The text was updated successfully, but these errors were encountered: