yiisoft / yiisoft/db-migration

Better constraints management in migrations

Open
#1 19 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
32
Forks
18
Avg merge
22h 18m
Merged PRs (30d)
2

Description

Currently we have to specify foreign key name with both adding and dropping.

$this->addForeignKey(
    'tests_questions_test_id_fkey',
    'tests_questions',
    'test_id',
    'tests',
    'id',
    'CASCADE',
    'CASCADE'
);
$this->dropForeignKey('tests_questions_test_id_fkey', 'tests_questions');

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:

  1. Foreign key names will be generated automatically so we can just write this:
$this->addForeignKey(
    'tests_questions',
    'test_id',
    'tests',
    'id',
    'CASCADE',
    'CASCADE'
);
  1. And for the dropping part we can specify relations something like that:
$this->dropForeignKey('tests_questions', ['test_id' => 'tests.id']);

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the addForeignKey and dropForeignKey migration entry points described in the issue, then compare how constraint names and relation metadata are handled across supported DBMSs. Determine whether automatic naming and relation-based dropping can work consistently; done means an agreed, testable scope for foreign-key constraint management.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.