question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I want to create a table basing on following MySQL dump:

CREATE TABLE `my_table` (
  `cmdId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `deviceId` char(16) NOT NULL,
  `fnNumber` int(10) unsigned DEFAULT NULL,
  `chNumber` int(10) unsigned DEFAULT NULL,
  `cmd` varchar(50) DEFAULT NULL,
  `cmdDate` datetime DEFAULT NULL,
  `delivered` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`cmdId`,`deviceId`),
  KEY `deviceId` (`deviceId`),
  CONSTRAINT `tblcommands_ibfk_1` FOREIGN KEY (`deviceId`) REFERENCES `second_table` (`deviceId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

My knex looks like:

knex.schema.createTable('my_table', function(t) {
    t.primary(['cmdId', 'deviceId']);
    t.increments('cmdId');
    t.string('deviceId', 16).notNullable().references('second_table.deviceId');
    t.integer('fnNumber').unsigned();
    t.integer('chNumber').unsigned();
    t.string('cmd96', 50);
    t.dateTime('cmdDate');
    t.boolean('delivered');
});

However there is a problem with primary keys. I get following error:

Error: Multiple primary key defined

I assume that increments method already creates PK and primary method creates another one which causes an error.

Is it possible to achieve this format of table ?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:9
  • Comments:36 (9 by maintainers)

github_iconTop GitHub Comments

23reactions
abeginescommented, Dec 7, 2016

Or you can create de autoincrement column with

table.specificType('myid','serial');

(only for Postgresql)

But I think is much better table.increments() will create the PK only if i manually specify that with table.increments().primary()

8reactions
mjurinciccommented, Mar 26, 2019

Any news on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple primary keys with one auto increment laravel
I have to migrate this database with Laravel to MySQL, but it seems that I can't use multiple primary ...
Read more >
Add a Primary Key and Auto-Increment Fields - CustomGuide
For this reason, many people use an AutoNumber field as their primary key. AutoNumber fields automatically add a new, unique number to each...
Read more >
MySQL: Why is auto_increment limited to just primary keys?
Two reasons why you must emulate this: MySQL accommodates only one increment column per table as does PostgreSQL, Oracle, SQL Server, and MS ......
Read more >
Two Primary Keys - Laracasts
The tutorial uses multiple primary keys in the pivot tables. ... You can't create 2 primary index, but you cant create compound primary...
Read more >
Setting up MySQL Auto Increment Primary Key 101 - Hevo Data
With Standard SQL support, you can quickly query, manipulate & add data to your MySQL Tables. One of the important tasks while creating...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found