Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create multiple PKs with increments #4903

Merged
merged 8 commits into from Jan 3, 2022

Conversation

OlivierCavadenti
Copy link
Collaborator

  • Resolve the case when a increments column is created in same time that primary keys. Now you can specify a increments columns with or without primary keys:
    • If a primary key is specified, the increment column is added to composite key,
    • If a primary key is not specified, the increment column is set to primary key.

This two cases works :

knex.schema.createTable('table_multiple_keys', function (t) {
  t.primary(['id', 'second_id', 'other_col']);
  t.increments('id');

  t.string('second_id', 16).notNullable();
  t.integer('other_col').notNullable();
});
knex.schema.createTable('table_multiple_keys', function (t) {
  t.primary(['second_id', 'other_col']);
  t.increments('id');

  t.string('second_id', 16).notNullable();
  t.integer('other_col').notNullable();
});
  • Solve tricky cases :

    • SQLite : it's not possible to specify autoincrement with composite key, autoincrement need to be primary key. I solve it by transform primary keys in unique index if autoincrement with primary keys is specified.
    • MYSQL don't support autoincrement without primary keys so it's not possible to add autoincrement column without primary key and alter table to add primary key in second query. I solve it by add classic int column, add composite primary key and transform the column to autoincrement.
  • Closes Cannot create multiple PKs (mixin .increments with .primary) #385

@kibertoad
Copy link
Collaborator

@OlivierCavadenti Great job, as always! Do you think this needs additional documentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot create multiple PKs (mixin .increments with .primary)
2 participants