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 Add Foreign Key Constraint

See original GitHub issue

When 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:closed
  • Created 9 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

61reactions
tgriessercommented, Apr 17, 2014

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.

60reactions
kamronbatmancommented, Jul 23, 2016

As an FYI to future people reading this: table.integer('UserId',11).unsigned().inTable('User').references('UserId'); is now table.integer('UserId',11).unsigned().references('UserId').inTable('User');

Read more comments on GitHub >

github_iconTop 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 >

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