migrations icon indicating copy to clipboard operation
migrations copied to clipboard

Cake Migrations support References of tables Fathers and Childs

Open AndreLZGava opened this issue 8 years ago • 2 comments

This is a (multiple allowed):

  • [ ] bug

  • [ ] enhancement

  • [x] feature-discussion (RFC)

  • Bake plugin version: Cake migrations should support table references like Rails, this way would be very usefull. If this exists please document.

Expected Behavior

When developer executs this command

cake bake migration CreateTableUser name:text category_id:references

This would create a migration, on execute it a table User user would be created with it's fields and a field category_id referencing category as foreign key.

Actual Behavior

This feature probably don't exist if it exists this isn't documented.

AndreLZGava avatar Jun 07 '17 17:06 AndreLZGava

@markstory Is this related to the undocumented references part?

dereuromark avatar Nov 30 '24 17:11 dereuromark

There isn't any support for references yet. It could be added though.

markstory avatar Dec 02 '24 15:12 markstory

Do we want to tackle this for 5.x?

dereuromark avatar Aug 09 '25 10:08 dereuromark

I don't think this has to wait for 5.x, it could be done in 4.x as new functionality to the column DSL. I have a few questions around the expected behavior though.

  1. If a column is created like thing_id:references how is the column type decided? Is the column an integer by default, should the column type be inferred from the primary key of things?
  2. Are there any default behaviors for update and delete clauses?

markstory avatar Aug 09 '25 22:08 markstory

Would the following make sense?

  • When a field uses :references or :reference type, parse it and extract:
    • Field name (e.g., category_id)
    • Referenced table (inferred from field name: category_id → categories)
    • Column type (default to integer for the foreign key column)
  • Allow optional syntax for specifying the referenced table:
    • category_id:references → references categories table
    • owner_id:references[users] → references users table (custom table name)
  1. Update BakeMigrationCommand (src/Command/BakeMigrationCommand.php)
  • Separate foreign key definitions from regular fields
  • Pass foreign key data to the template alongside columns
  • Generate both: a. The column definition (e.g., category_id as integer) b. The foreign key constraint
  1. Enhance Migration Template (templates/bake/config/skeleton.twig)
  • Add a section to generate addForeignKey() calls
  • Support both builtin and phinx backends
  • Include proper options for cascading deletes/updates
  1. Implementation Details

Column Grammar Extension:

  {name}:{type}[{referenced_table}]{nullable}:{index}

Examples:

Basic reference (infers categories table)

  bin/cake bake migration CreatePosts title:string category_id:references

Nullable reference

  bin/cake bake migration CreatePosts title:string category_id:references?

Custom referenced table

  bin/cake bake migration CreatePosts title:string owner_id:references[users]

Reference with cascade delete

  bin/cake bake migration CreatePosts title:string category_id:references:cascade

Generated Migration (Builtin Backend):

  public function change(): void
  {
      $table = $this->table('posts');
      $table->addColumn('title', 'string', ['null' => false])
          ->addColumn('category_id', 'integer', ['null' => false])
          ->addForeignKey(
              $this->foreignKey('category_id')
                  ->setReferencedTable('categories')
                  ->setReferencedColumns('id')
                  ->setOnDelete('RESTRICT')
                  ->setOnUpdate('NO_ACTION')
          )
          ->create();
  }

dereuromark avatar Oct 16 '25 13:10 dereuromark