Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create record with composite primary key #405

Open
garcia-marco opened this issue Apr 20, 2020 · 3 comments
Open

Can't create record with composite primary key #405

garcia-marco opened this issue Apr 20, 2020 · 3 comments

Comments

@garcia-marco
Copy link

garcia-marco commented Apr 20, 2020

Hello

Expected behavior

Create or edit a record in a table with 3 primary keys.

Actual behavior

It doesn't create or edit a record

Failure Logs

*TABLE NAME* creation failed: PRIMARY must be unique

Context

I'm on my local environment.

  • Package Version: 6.0.0
  • Express Version: 4.16.3
  • Sequelize Version: 5.15.1
  • Database Dialect: MySQL
  • Database Version: 5.7.24
@garcia-marco
Copy link
Author

Up

@arnaudbesnier
Copy link
Member

Hi @garcia-marco,

Can you provide more details about your issue.
Forest Admin is supposed to support record creations on tables having composite primary keys.

Looking at the failure log *TABLE NAME* creation failed: PRIMARY must be unique, I am wondering if it is not the expected behaviour if you try to insert a row that contains the primary keys of an existing row in the table.

Let us know.

@garcia-marco
Copy link
Author

Hi, I'm sorry I forgot to answer at the time.
I remember that when I was trying to create a record with 3 primary keys like :

primary_key_1 primary_key_2 primary_key_3
1 1 1

It wasn't working if there was an existing row with one of the primary_key like :

primary_key_1 primary_key_2 primary_key_3
1 2 2

I fixed it by setting only one primary key "id" that I added at the time

This was my model at the time:

module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  const Table = sequelize.define('table', {
    first_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    second_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    third_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      defaultValue: 1,
    }
  }, {
    tableName: 'table',
    underscored: true,
    underscoredAll: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  });
  Table.removeAttribute('id');
  Table.associate = (models) => {
    Table.belongsTo(models.first, {
      foreignKey: 'first_id'
    })
    Table.belongsTo(models.second, {
      foreignKey: 'second_id'
    })
    Table.belongsTo(models.third, {
      foreignKey: 'third_id'
    })
  };
  return Table;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants