db
db copied to clipboard
Add / document a more convenient way to specify composite primary keys
The way that was advised by @darkdef
$qb->createTable(
'test',
[
'name' => 'varchar(255) NOT NULL',
'email' => 'varchar(255) NOT NULL',
'PRIMARY KEY (name, email)',
],
);
PRIMARY KEY (name, email):
- This is not a column.
- It's pretty basic for writing raw SQL.
Funding
- You can sponsor this specific effort via a Polar.sh pledge below
- We receive the pledge once the issue is completed & verified
Cycle implementation just for comparison:
$schema = $table->getSchema();
$schema->string('parent', 128)->nullable(false);
$schema->string('child', 128)->nullable(false);
$schema->setPrimaryKeys(['parent', 'child']);
$schema->save();
In Yii2 we decided that intentionally so when reviewing a database these names would make sense.
Automatic names when generated in a semantical way could:
- Reach character limit. That is not uncommon for MySQL.
- Collide.
That's why Doctrine when generating migrations via diff uses names such as UNIQ_56AB981964D218E or IDX_5A7E7FD6419054B4. That solves the issue technically but it becomes an unreadable mess to the one who will inspect the database itself later.