Cannot Add Foreign Key Constraint
See original GitHub issueWhen running a migration with the below code, I get an “Cannot add foreign key contraint” message. Anyone point me in the right direction?
exports.up = function(knex, Promise) {
var defer=Promise.defer();
knex.schema.createTable('User',function(table){
//--Create User Table
table.increments('UserId').primary();
table.string('username');
table.string('email',60);
table.string('password',65);
table.timestamps();
})
.then(function(){
return knex.schema.createTable('Comment',function(table){
//--Create Comment Table
table.increments('CommentId').primary();
table.string('Comment');
table.integer('UserId',11).inTable('User').references('UserId');
});
})
.then(function(){
defer.resolve();
});
return defer.promise;
};
exports.down = function(knex, Promise) {
};
Issue Analytics
- State:
- Created 9 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
MySQL Error Code 1215: "Cannot add foreign key constraint"
MySQL Error Code 1215: “Cannot add foreign key constraint” · 1) The table or index the constraint refers to does not exist yet...
Read more >MySQL Cannot Add Foreign Key Constraint - Stack Overflow
The cause was: Since i used phpmyadmin to create some foreign keys in the renamed database - the foreign keys where created with...
Read more >How to fix MySQL error 1215 Cannot add foreign key constraint
When adding foreign key constraints, the referenced column and the referencing column must both have the same data type. An important tip here ......
Read more >Fix : MYSQL Cannot Add Foreign Key Constraint - 5 Balloons
This query will return three fields Type, Name and Status. Expand the status column of the result and look for LATEST FOREIGN KEY...
Read more >General error: 1215 Cannot add foreign key ... - Laracasts
The issue was that either mysql didn't want foreign keys during table creation, or laravel was issuing them in the wrong order. In...
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
Assuming you might be using mysql, you need to specify that the
UserId
is.unsigned()
You also shouldn’t need that defer there, you can just return the
knex.schema.createTable
.As an FYI to future people reading this:
table.integer('UserId',11).unsigned().inTable('User').references('UserId');
is nowtable.integer('UserId',11).unsigned().references('UserId').inTable('User');