Cannot create multiple PKs (mixin .increments with .primary)
See original GitHub issueI 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:
- Created 9 years ago
- Reactions:9
- Comments:36 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Or you can create de autoincrement column with
(only for Postgresql)
But I think is much better
table.increments()
will create the PK only if i manually specify that withtable.increments().primary()
Any news on this?