# Can't create new entry with custom relationships

**URL:** <https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271>\
**Category:** Help me!\
**Created:** [November 8, 2020, 6:15am UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271 "2020-11-08T06:15:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LotusBlack](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/lotusblack/32/881_2.png) [@LotusBlack](https://community.forestadmin.com/u/LotusBlack)\
**Post date:** [November 8, 2020, 6:15am UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/1 "2020-11-08T06:15:52Z")

</div>

Hi 👋,

## 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](https://community.forestadmin.com/t/trouble-with-custom-relationship-table/740/52).  
Especially this portion of answer:

```javascript
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

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 9, 2020, 9:23am UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/2 "2020-11-09T09:23:44Z")

</div>

Hi @LotusBlack 👋,

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.

---

<div class="post-metadata">

**Author:** ![LotusBlack](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/lotusblack/32/881_2.png) [@LotusBlack](https://community.forestadmin.com/u/LotusBlack)\
**Post date:** [November 9, 2020, 6:54pm UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/3 "2020-11-09T18:54:43Z")

</div>

Hi @anon39848301,  
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:

```javascript
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](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9a3891136875cb724cdb020213dcd678738f4652.png)  
And you will see that `rm_supplier_per_manufacturer` don’t use the `id` in ForestAdmin but a composite key from the 2 FK.

---

<div class="post-metadata">

**Author:** ![rap2h](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/rap2h/32/25_2.png) [@rap2h](https://community.forestadmin.com/u/rap2h)\
**Post date:** [November 10, 2020, 9:58am UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/4 "2020-11-10T09:58:50Z")

</div>

Hi @LotusBlack

I just reproduced the problem, thanks to your detailed explanation 🙏

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

### Create a database

```sql
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

```sql
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 🐛

 ![Capture d’écran 2020-11-10 à 10.50.36](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/aba27c1bf3f95c4aba38d9dc87ac3b3adb47b745.png)

👉 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.

---

<div class="post-metadata">

**Author:** ![rap2h](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/rap2h/32/25_2.png) [@rap2h](https://community.forestadmin.com/u/rap2h)\
**Post date:** [November 10, 2020, 3:14pm UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/5 "2020-11-10T15:14:49Z")

</div>

https://share.clickup.com/t/h/a4xnh6/4LJJ73BIF5HHA3M

---

<div class="post-metadata">

**Author:** ![LotusBlack](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/lotusblack/32/881_2.png) [@LotusBlack](https://community.forestadmin.com/u/LotusBlack)\
**Post date:** [November 10, 2020, 3:28pm UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/6 "2020-11-10T15:28:51Z")

</div>

Hi @rap2h,

Thanks for putting the bug in your backlog.

I am waiting for your update 🙂.

---

<div class="post-metadata">

**Author:** ![arnaud](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud/32/32_2.png) [@arnaud](https://community.forestadmin.com/u/arnaud)\
**Post date:** [January 11, 2021, 3:37pm UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/7 "2021-01-11T15:37:42Z")

</div>

Hi @LotusBlack

A patch has been released for this issue (according to the status of ticket posted above)  
Let me set this response as the “Solution”.

Of course, feel free to tell us, if it does fix your issue on your project.

---

<div class="post-metadata">

**Author:** ![LotusBlack](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/lotusblack/32/881_2.png) [@LotusBlack](https://community.forestadmin.com/u/LotusBlack)\
**Post date:** [January 20, 2021, 8:51pm UTC](https://community.forestadmin.com/t/cant-create-new-entry-with-custom-relationships/1271/8 "2021-01-20T20:51:29Z")

</div>

Hi @arnaud,

Thanks! The issue is resolved on my side.
