# Implicit relationship ManyToMany triggering error in Forest - NestJs

**URL:** <https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287>\
**Category:** Help me!\
**Created:** [September 22, 2022, 6:02am UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287 "2022-09-22T06:02:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![walidthebest](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/walidthebest/32/4861_2.png) [@walidthebest](https://community.forestadmin.com/u/walidthebest)\
**Post date:** [September 22, 2022, 6:02am UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/1 "2022-09-22T06:02:20Z")

</div>

## Feature(s) impacted

Displaying many to many relation with no primary id in the dashboard.  
I do understand the error, I just want to find a solution that won’t force me to modify the database to get it work. Please can you tell me if there is one ?  
PS : by the way, where can we find the whole documentation for NestJs.

## Observed behavior

I can not reach the data : the message display in the backend is  
**This collection has no primary key**

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8584f75a683a706cf53bd9af8d7ad0606534d2ab.png)

I am using Forest Admin with NestJs

## Expected behavior

I want to be able to reach the data and add Tags to Event in my case (see screenshot)

## Failure Logs

```javascript
 This collection has no primary key 

Error: This collection has no primary key
    at Function.packId (/home/wghanem/repo/silencio-backoffice/node_modules/@forestadmin/agent/src/agent/utils/id.ts:20:13)
    at Object.beforeSerialize (/home/wghanem/repo/silencio-backoffice/node_modules/@forestadmin/agent/src/agent/services/serializer.ts:83:33)
    at JSONAPISerializer.serializeResource (/home/wghanem/repo/silencio-backoffice/node_modules/json-api-serializer/lib/JSONAPISerializer.js:565:22)
    at /home/wghanem/repo/silencio-backoffice/node_modules/json-api-serializer/lib/JSONAPISerializer.js:560:14
    at Array.map (<anonymous>)
    at JSONAPISerializer.serializeResource (/home/wghanem/repo/silencio-backoffice/node_modules/json-api-serializer/lib/JSONAPISerializer.js:559:19)
    at JSONAPISerializer.serialize (/home/wghanem/repo/silencio-backoffice/node_modules/json-api-serializer/lib/JSONAPISerializer.js:110:27)
    at Serializer.serialize (/home/wghanem/repo/silencio-backoffice/node_modules/@forestadmin/agent/src/agent/services/serializer.ts:19:51)
    at Serializer.serializeWithSearchMetadata (/home/wghanem/repo/silencio-backoffice/node_modules/@forestadmin/agent/src/agent/services/serializer.ts:34:26)
    at ListRelatedRoute.handleListRelated (/home/wghanem/repo/silencio-backoffice/node_modules/@forestadmin/agent/src/agent/routes/access/list-related.ts:38:54)

```

## Context

- Project name: Silencio
- Team name: Silencio
- Environment name: Production
- Agent type & version: Liana Version 1.0.0-beta.58

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [September 22, 2022, 8:57am UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/2 "2022-09-22T08:57:34Z")

</div>

Hi @walidthebest

Can you give me the structure of the tables which are being used here so that I have a better understanding of the issue?

The documentation about many to many relationships is here: [To multiple records - [Beta] Developer guide](https://docs.forestadmin.com/beta-developer-guide-agents-v2/agent-customization/relationships/multiple-records)

---

<div class="post-metadata">

**Author:** ![walidthebest](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/walidthebest/32/4861_2.png) [@walidthebest](https://community.forestadmin.com/u/walidthebest)\
**Post date:** [September 22, 2022, 9:11am UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/3 "2022-09-22T09:11:26Z")

</div>

Here it is (simplified model):

```javascript
model Tag {
    id Int @id @default(autoincrement())
}

model Event {
    id Int @id @default(autoincrement())
}

model _TagToEvent {
   tagId Int 
   eventId Int 

  @index([tagId,eventId])
  @unique([tagId,eventId])
}

```

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [September 22, 2022, 9:32am UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/5 "2022-09-22T09:32:57Z")

</div>

There is no easy way to fix that…  
ForestAdmin needs a PK to be able to address the \_TagToEvent table, and that’s a hard requirement.

As you have a unique index, the easier way would be to hook into the database introspection step, and tell forest admin to use [tagId, eventId] as a PK (composite pks are supported), but their is no way to do that right now.

If you don’t need this to work right now, we have a [pull request which is currently in review](https://github.com/ForestAdmin/agent-nodejs/pull/434) which was implemented so that customers can cache the result of the database introspection (that would allow faster startup times for the agent).

Once merged this would allow you to do something like this.

```javascript
import { createSqlDataSource, introspect } from '@forestadmin/datasource-sql';

const uri = 'postgres://...';
const introspection = await introspect(uri);

// Change the output of the introspection so that forest thinks tagId
// and eventId are primary keys
introspection
  .find(t => t.name === '_TagToEvent')
  .columns
  .forEach(c => { c.primaryKey = c.name.endsWith('Id') });

createAgent(options)
  .addDataSource(createSqlDataSource(uri, { introspection }))

```

Would that be OK for you?

---

<div class="post-metadata">

**Author:** ![walidthebest](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/walidthebest/32/4861_2.png) [@walidthebest](https://community.forestadmin.com/u/walidthebest)\
**Post date:** [September 22, 2022, 2:11pm UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/6 "2022-09-22T14:11:23Z")

</div>

I’ll add an id to relations 🙂 thank you

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [September 22, 2022, 2:26pm UTC](https://community.forestadmin.com/t/implicit-relationship-manytomany-triggering-error-in-forest-nestjs/5287/7 "2022-09-22T14:26:18Z")

</div>

Just to be clear, there is no need to add a new column.

You can simply mark both keys as pks
