0

Trying to create a very simple proof-of-concept to create table associations with migrations, but cannot add a foreign key when running through the cli - ./node_modules/.bin/sequelize db:migrate

Made sure to clear out my SequelizeMeta & SequelizeData tables and reset everything before running, but still get the error.

Migrations run fine when I remove the userId field definition in create-company.js

create-user.js

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      firstName: {
        type: Sequelize.STRING,
      },
      lastName: {
        type: Sequelize.STRING,
      },
      email: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('users');
  },
};

create-company.js

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('companies', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      name: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      userId: {
        type: Sequelize.UUID, // have also tried Sequelize.INTEGER
        references: {
          model: 'users',
          key: 'id',
        },
      },
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('companies');
  },
};

What do you expect to happen?

Create a foreign key field in my companies table

What is actually happening?

Get the following output:

Sequelize CLI [Node: 9.7.1, CLI: 4.0.0, ORM: 4.36.0]

Loaded configuration file "src/database/config.js".
Using environment "dev".
== 20180309004031-create-user: migrating =======
== 20180309004031-create-user: migrated (0.036s)

== 20180316173751-create-company: migrating =======

ERROR: Cannot add foreign key constraint

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! setup1@1.0.0 migrate:dev: `NODE_ENV=dev ./node_modules/.bin/sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the setup1@1.0.0 migrate:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/shoma/.npm/_logs/2018-03-16T18_22_27_398Z-debug.log

Dialect: mysql innoDB utf8 Dialect version: 5.7.21 Homebrew Database version: 5.7.21 Homebrew Sequelize version: Sequelize 4.35.2, Sequelize CLI version: Sequelize-cli 4.0.0 Tested with latest release: No (If yes, specify that version)

snn
  • 405
  • 5
  • 15

1 Answers1

4

Referenced field must be same types. But you have "company.userId: UUID" and "user.id: INTEGER".