db icon indicating copy to clipboard operation
db copied to clipboard

Add / document a more convenient way to specify composite primary keys

Open arogachev opened this issue 2 years ago • 2 comments

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
Fund with Polar

arogachev avatar Apr 07 '23 05:04 arogachev

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();

arogachev avatar Apr 07 '23 05:04 arogachev

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:

  1. Reach character limit. That is not uncommon for MySQL.
  2. 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.

samdark avatar Apr 07 '23 06:04 samdark