migrations
migrations copied to clipboard
Cake Migrations support References of tables Fathers and Childs
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.
@markstory Is this related to the undocumented references part?
There isn't any support for references yet. It could be added though.
Do we want to tackle this for 5.x?
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.
- If a column is created like
thing_id:referenceshow is the column type decided? Is the column an integer by default, should the column type be inferred from the primary key ofthings? - Are there any default behaviors for
updateanddeleteclauses?
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)
- 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
- 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
- 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();
}