Sequelize foreign key error on Heroku, but not local testing

I am a new developer and am trying to teach myself Sequelize and mysql with a few small test projects. What I have now is a small RPG team strength analyzer. I have a SQL table of units that has a schema (id, name, elementOne, elementTwo) - integer, string, string, string.

Currently the elementOne and ElementTwo tables are the same 18 string values ​​because I couldn't figure out how to set up a Sequelize query with foreign key refs to the same table (just "elements" for example).

Adding to the Unit table works fine on local server, but only breaks on Heroku when trying to add a third device with the following error:

Error was:  { SequelizeForeignKeyConstraintError: Cannot add or update a child  
row: a foreign key constraint fails (`heroku_f4daeab1e260595`.`units`, 
CONSTRAINT `units_ibfk_1` FOREIGN KEY (`id`) REFERENCES `elementtwos` (`id`) 
ON DELETE CASCADE ON UPDATE CASCADE)

      

Here are all the tables and relationship declarations.

const Unit = sequelize.define('unit', {
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    unique: true,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: false
  },
  image: {
    type: Sequelize.STRING,
    allowNull: true,
    unique: false
  },
  elementOne: {
    type: Sequelize.INTEGER,
    allowNull: true,
    references: {
      model: Element,
      key: 'id'
    }
  },
  elementTwo: {
    type: Sequelize.INTEGER,
    allowNull: true,
    defaultValue: 10001,
    references: {
      model: ElementTwo,
      key: 'id'
    }
  }
});

const Element = sequelize.define('element', {
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    unique: true,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: false
  }
});
const ElementTwo = sequelize.define('elementtwo', {
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    unique: true,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: false
  }
});

      

After they all downloaded, I installed the following:

Unit.belongsTo(Element, {foreignKey: 'elementOne'});
Unit.belongsTo(ElementTwo, {foreignKey: 'elementTwo'});
ElementTwo.hasMany(Unit, {foreignKey: 'id'});
Element.hasMany(Unit, {foreignKey: 'id'});

      

And this is the request that Sequelize does (in Unit.create ({...}):

INSERT INTO `units` 
(`id`,`name`,`image`,`elementOne`,`elementTwo`,`createdAt`,`updatedAt`) VALUES 
 (DEFAULT,'raichu','http://longimgurl.png',13,10001,'2017-06-14 
12:57:54','2017-06-14 12:57:54');

      

If anyone can offer any advice, we would be very grateful.

+3


source to share


1 answer


this is a mysql error and because you are defining a foreign key constraint on your table and you are trying to insert an invalid value in the fk field that does not come out of the target table,



check element

and elementTwo

a table and make sure that these values are available

+1


source







All Articles