{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/8163","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/8163/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/8163/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/8163/events","html_url":"https://github.com/yiisoft/yii2/issues/8163","id":70373500,"number":8163,"title":"Better constraints management in migrations","user":{"login":"arogachev","id":8326201,"avatar_url":"https://avatars.githubusercontent.com/u/8326201?v=3","gravatar_id":"","url":"https://api.github.com/users/arogachev","html_url":"https://github.com/arogachev","followers_url":"https://api.github.com/users/arogachev/followers","following_url":"https://api.github.com/users/arogachev/following{/other_user}","gists_url":"https://api.github.com/users/arogachev/gists{/gist_id}","starred_url":"https://api.github.com/users/arogachev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arogachev/subscriptions","organizations_url":"https://api.github.com/users/arogachev/orgs","repos_url":"https://api.github.com/users/arogachev/repos","events_url":"https://api.github.com/users/arogachev/events{/privacy}","received_events_url":"https://api.github.com/users/arogachev/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-04-23T10:33:37Z","updated_at":"2015-04-24T10:32:29Z","closed_at":null,"body":"Currently we have to specify foreign key name with both adding and dropping.\r\n\r\n```php\r\n$this->addForeignKey(\r\n    'tests_questions_test_id_fkey',\r\n    'tests_questions',\r\n    'test_id',\r\n    'tests',\r\n    'id',\r\n    'CASCADE',\r\n    'CASCADE'\r\n);\r\n```\r\n```php\r\n$this->dropForeignKey('tests_questions_test_id_fkey', 'tests_questions');\r\n```\r\n\r\nSome DBMS like MySQL generate it automatically, but the dropping part for me is more frustrating.\r\nYou have to look through all previous migrations and find the name of corresponding foreign key.\r\nOne solution will be following some convention and ask team members to follow it too, but it will be great if:\r\n\r\n1) Foreign key names will be generated automatically so we can just write this:\r\n\r\n```php\r\n$this->addForeignKey(\r\n    'tests_questions',\r\n    'test_id',\r\n    'tests',\r\n    'id',\r\n    'CASCADE',\r\n    'CASCADE'\r\n);\r\n```\r\n\r\n2) And for the dropping part we can specify relations something like that:\r\n\r\n```php\r\n$this->dropForeignKey('tests_questions', ['test_id' => 'tests.id']);\r\n```\r\n\r\nAnd foreign key will be found and dropped automatically.\r\n\r\nThat way developer only cares about relations and not names.\r\n\r\nI think It can be applied to primary keys, indices, etc. too.\r\n\r\nWhat do you think? Is it possible with DBMS?\r\n\r\nMaybe 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."},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/95884623","html_url":"https://github.com/yiisoft/yii2/issues/8163#issuecomment-95884623","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/8163","id":95884623,"user":{"login":"omnilight","id":3306537,"avatar_url":"https://avatars.githubusercontent.com/u/3306537?v=3","gravatar_id":"","url":"https://api.github.com/users/omnilight","html_url":"https://github.com/omnilight","followers_url":"https://api.github.com/users/omnilight/followers","following_url":"https://api.github.com/users/omnilight/following{/other_user}","gists_url":"https://api.github.com/users/omnilight/gists{/gist_id}","starred_url":"https://api.github.com/users/omnilight/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omnilight/subscriptions","organizations_url":"https://api.github.com/users/omnilight/orgs","repos_url":"https://api.github.com/users/omnilight/repos","events_url":"https://api.github.com/users/omnilight/events{/privacy}","received_events_url":"https://api.github.com/users/omnilight/received_events","type":"User","site_admin":false},"created_at":"2015-04-24T10:32:29Z","updated_at":"2015-04-24T10:32:29Z","body":"Absolutely aggree, currently it is a pain to write foreight keys every time"}}