Composite primary key

Hi,

We are having issues setting models with a composite primary key and accessing these through related data.

This is one of the models we are having a problem on, in the db we have this index "enterprise_groups_accounts_pkey" PRIMARY KEY, btree (enterprise_group_id, account_id):

At this point, all good we can access the Enterprise Groups Accounts table

The problem comes when set a simple hasMany on a related table, as such on the accounts model:

this.hasMany(models.enterprise_groups_accounts, {
      foreignKey: 'account_id'
})

Expected behavior

We should be able to access to account enterprise groups account from the account table

Actual behavior

Cannot reach your data Error

More precisely this error in the console:

When investing I realized that recordSerializer.serialize(data) will not set the id properly.

From the types defined I am supposed to expect data looking like this:

StatSerialized.data: {
    type: string;
    id: string;
    attributes: {
        value: any[];
    };
}

But here we do not have an id field.

Context

  • Package Version: 6.3.6
  • Express Version: “forest-express-sequelize”: “^7.8.0”,
  • Sequelize Version: “sequelize”: “~5.15.1”
  • Database Dialect: postgresql
  • Project Name: Commutifi

Hey @arthur :wave:,

Composite PKs are not well supported. I’ve created a ticket in our issue tracked for this one. I know that’s far from perfect, but adding an id column on your model table is a workaround.

We use record ids for most of href links in the app, and not having one will definitely cause issues in the long run.

I’ll also push the compose key support to our product board.

Let me know if that helps

Perfect,

Will add an ID for forestadmin in our models.

Thanks for your help :grinning:

1 Like

I’ll mark this thread as resolved then, even though I know the solution is far from being perfect in your case.

We’ll let you know once we tackle this issue, and let us know if you still experience any issue then :raised_hands:

1 Like