Can't create new entry with custom relationships

Hi :wave:,

Expected behavior

When having a table with 2 FK, I want the ID to be the one in the table and not a concatenation of 2 FK.
My issue seems strongly linked to this one.
Especially this portion of answer:

jeffladiray
Sep 16

[...] It seems that the id column is missing, which seems to correspond with the fact that you are getting a composite id instead of your default id.

Actual behavior

The model with 2 FK has a concatenation primary key, like 1|1, and thus, can not be linked with anything (I have an error message when I want to link an entry to another model).

What I have tried

  • Add the field ID as a PK in the model with 2 FK in order to force the primary be to not be composite id.`It works nicely but now I have to enter manually the ID each time. Not usable.
  • Add the field ID directly in the .forest-schema.json file. It did not stay in my dev environment and was deleted each time.
  • Add a field IdForestAdmin in my Postgres Schema and add this field in the model as a PK. It works, but again, not usable.

Context

  • Package Version:lumber-cli@3.9
  • Express Version: 4.16.4
  • Sequelize Version: 5.15.2
  • forest-express-sequelize: 6.3.14

Hi @LotusBlack :wave:,

What do you mean by “not usable”?

Using a postgres generated sequence or a column of Serial type should allow you to avoid entering an id each time.
We do partially support composite primary keys in a few cases, but if you need to frequently manipulate these records, adding an id column is the best solution.

Let me know if that helps.

Hi @jeffladiray,
thanks for your answer.

I was not very clear, I already have a serial Id in my DB. But even with my ID in my db, the primary key used is a composite one.

My first point was that I had to explicitly put the field in the js model in ForestAdmin in order to force it to take it as the pk and not a composite key. I had to do like this:

id: {
  type: DataTypes.INTEGER,
  primaryKey: true,
}

But again, this is not usable as I have to manually enter the value each time I am adding a new entry (even if this is a serial in my db).

I just want to use my serial ID as the pk. But ForestAdmin doesn’t seem to take it by default?

It is not difficult to reproduce, you need to have an architecture like that:
image
And you will see that rm_supplier_per_manufacturer don’t use the id in ForestAdmin but a composite key from the 2 FK.

1 Like

Hi @LotusBlack

I just reproduced the problem, thanks to your detailed explanation :pray:

:point_right: Here are the steps to reproduce this issue (I leave it here for furtherer readers):

Create a database

create table if not exists rm_suppliers (
	id serial not null constraint rm_suppliers_pk primary key,
	name varchar,
	created_at timestamp,
	updated_at timestamp);

create table if not exists rm_manufacturers (
	id serial not null constraint rm_manufacturers_pk primary key,
	name varchar,
	created_at timestamp,
	updated_at timestamp);

create table if not exists rm_supplier_per_manufacturer (
	id serial not null constraint rm_supplier_per_manufacturer_pk primary key,
	id_supplier integer constraint rm_supplier_per_manufacturer_rm_suppliers_id_fk references rm_suppliers,
	id_manufacturer integer constraint rm_supplier_per_manufacturer_rm_manufacturers_id_fk references rm_manufacturers,
	updated_at timestamp,
	deleted_at timestamp);

Add some data

insert into rm_manufacturers (id, name, created_at, updated_at) values (1, 'm1', '2020-11-10 10:16:13.000000', '2020-11-10 10:16:15.000000');
insert into rm_suppliers (id, name, created_at, updated_at) values (1, 's1', '2020-11-10 10:16:36.000000', '2020-11-10 10:16:38.000000');
insert into rm_supplier_per_manufacturer (id, id_supplier, id_manufacturer, updated_at, deleted_at) values (1, 1, 1, '2020-11-10 10:16:48.000000', '2020-11-10 10:16:49.000000');

Create a new project

Go to Forest Admin UI, create a new project, update lumber to the latest version then run the generate command.

Go to your project in Forest Admin UI

The ID used by Forest Admin is the composite key :bug:

:point_right: We have to investigate now. Thanks to your message, we have a context, and we can now reproduce, analyze, and hopefully fix the problem. Be sure we will go back to you as soon as it has been fixed. Thank you for your patience.

3 Likes
1 Like

Hi @rap2h,

Thanks for putting the bug in your backlog.

I am waiting for your update :slightly_smiling_face:.

2 Likes