ckingchris
ckingchris

Reputation: 609

How to fix? Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "x" violates foreign key constraint "y_fkey"

This error is showing when I attempt to POST / create a new contact in the database. This is the error Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "contacts" violates foreign key constraint "org_name_fkey". Below are the models, does anyone know how to fix this? All of the other fields POST fine, it is only when attempted to add org_name. If I don't include org_name in the POST everything stores in the postgres database.

Contact Model

module.exports = (sequelize, DataTypes) => {
  const Contacts = sequelize.define('contact', {
      contact_id: {
        type: Sequelize.INTEGER,
        field: 'contact_id',
        primaryKey: 'true'
      },
      first_name: {
        type: Sequelize.STRING,
        field: 'first_name'
      },
      last_name: {
        type: Sequelize.STRING,
        field: 'last_name'
      },
      contact_type_id: {
        type: Sequelize.STRING,
        references: {
          model: 'contact_type',
          key: 'contact_type_id'
        }
      },
      org_name: {
        type: Sequelize.STRING,
        references: {
          model: 'org',
          key: 'org_name'
        }
      }
    },
    {
      tableName: 'contacts',
    }
  );

  Contacts.associate = (models) => {
    Contacts.belongsTo(models.contact_type, {foreignKey: 'contact_type_id'});
    Contacts.belongsTo(models.org, {foreignKey: 'org_name'});
  };

  return Contacts;
};

Contact Type Model

module.exports = (sequelize, DataTypes) => {
  const ContactType = sequelize.define('contact_type', {
      // attributes
      contact_type_id: {
        type: Sequelize.STRING,
        field: 'contact_type_id'
      },
      updated_at: {
        type: Sequelize.DATE,
        field: 'updated_at'        
      },
      created_at: {
        type: Sequelize.DATE,
        field: 'created_at'
      }
    },
  );

  ContactType.associate = (models) => {
    ContactType.hasOne(models.contact, {foreignKey: 'contact_id'});
  };

  return ContactType;
};

Org Model

module.exports = (sequelize, DataTypes) => {
  const Org = sequelize.define('org', {
      org_name: {
        type: Sequelize.STRING,
        field: 'org_name',
        primaryKey: 'true'
      },
      org_type_id: {
        type: Sequelize.STRING,
        field: 'org_type_id'
      },
      website: {
        type: Sequelize.STRING,
        field: 'website'
      }
    },
  );

  Org.associate = (models) => {
    Org.hasMany(models.contact, {foreignKey: 'contact_id'});
  };

  return Org;
};

Upvotes: 0

Views: 1571

Answers (1)

sunson1164
sunson1164

Reputation: 83

What that error tells you is that whatever org_name you are passing into your POST request does not have a corresponding Org record in the database. i.e. You are creating a person who works at ABC Corp, but you haven't added ABC Corp to your Orgs. You would have to fix this by creating the Org first, then the Contact.

You do not give the exact request that causes the error. This would be helpful in tracking down your issue. Also, you should query your database to find the state of the Org table before the POST. Perhaps you forgot to call save on the Org instance.

Upvotes: 2

Related Questions