user3440787
user3440787

Reputation: 193

Sequelize i18n: errno: 150 "Foreign key constraint is incorrectly formed")

I'm using "sequelize-i18n" for multilanguage support. I'm using sequelize with nodeJS and foud one package i18n to work with sequelize.

I followed the documentation but each time I'm getting error below.

  Unhandled rejection SequelizeDatabaseError: Can't create table `dev_****`.`product_i18ns` (errno: 150 "Foreign key constraint is incorrectly formed")
    at Query.formatError (C:\Users\****\learnings\node\****-acl\node_modules\sequelize\lib\dialects\mysql\query.js:247:16)
    at Query.handler [as onResult] (C:\Users\****\learnings\node\****-acl\node_modules\sequelize\lib\dialects\mysql\query.js:68:23)
    at Query.Command.execute (C:\Users\****\learnings\node\****-acl\node_modules\mysql2\lib\commands\command.js:30:12)
    at Connection.handlePacket (C:\Users\****\learnings\node\****-acl\node_modules\mysql2\lib\connection.js:502:28)
    at PacketParser.onPacket (C:\Users\****\learnings\node\****-acl\node_modules\mysql2\lib\connection.js:81:16)
    at PacketParser.executeStart (C:\Users\****\learnings\node\****-acl\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (C:\Users\****\learnings\node\****-acl\node_modules\mysql2\lib\connection.js:89:29)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onread (net.js:638:20)

My Product model code is below.

 'use strict';
module.exports = (sequelize, DataTypes) => {
  var Product = sequelize.define('Product', {
    id : {
      type          : DataTypes.BIGINT,
      primaryKey        : true,
      autoIncrement     : true
  },
    firstName: {

      type: DataTypes.STRING,
      i18n: true

    },
    lastName: DataTypes.STRING,
    email: DataTypes.STRING
  }, {});

  return Product;
};

My server file code is below to initialize the i18n

    'use strict';

var fs = require('fs');
var path = require('path');
var Acl = require('acl');
var session = require('express-session');
var Sequelize = require('sequelize');

var SequelizeI18N = require('sequelize-i18n');

var AclSeq = require('acl-sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
// var config    = require(__dirname + '/..\config\config.json')[env];
var config = require('./../config/config.json')[env];
var db = {};

var languages = { 
  list : ["EN" , "FR" , "ES"] , 
  default : "EN" 
};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

var acl = new Acl(new AclSeq(sequelize, { prefix: 'acl_' }));


// Init i18n
var i18n = new SequelizeI18N( sequelize, { languages: languages.list, default_language: languages.default } );
i18n.init();
fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Please help me out, is it the package issue as this package is not much popular.

Upvotes: 0

Views: 5182

Answers (1)

user3440787
user3440787

Reputation: 193

I found the solution myself after spending couple of hours, might be it would be helpful for others. Issue was mismatch dataType

in my model I created id type to BIGINT

id : {
      type : DataTypes.BIGINT}

But, when I checked in phpmyadmin it was INT(11) only. here was the issue. so I had to change the dataType to BIGINT and it is working perfectly.

Upvotes: 4

Related Questions