# ID of parent collection not filled in when creating a new record

**URL:** <https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846>\
**Category:** Help me!\
**Created:** [February 15, 2021, 3:36pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846 "2021-02-15T15:36:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 15, 2021, 3:36pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/1 "2021-02-15T15:36:30Z")

</div>

Hi team,

When trying to create a record in a collection linked to a parent collection, the id of the parent collection is not automatically populated.

Here is an example:

```javascript
db.users.hasMany(db.addresses);
db.addresses.belongsTo(db.users, { as: 'user', foreignKey: 'user_id' });

```

Please have a look at this video, I am trying to create an address record for my user, and the user ID is not populated.

Could you please confirm that this is a bug?  
I think that this was automatically populated some time ago.

Thanks in advance for your help

Here’s my setup

```javascript
    "database_type": "postgres",
    "liana": "forest-express-sequelize",
    "liana_version": "6.6.3",
    "engine": "nodejs",
    "engine_version": "12.13.1",
    "orm_version": "5.22.3"

```

---

<div class="post-metadata">

**Author:** ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)\
**Post date:** [February 15, 2021, 5:10pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/2 "2021-02-15T17:10:40Z")

</div>

Hi @Louis-Marie 👋 thank you for your feedback.  
You can simply omit the field and it will be automatically populate after you click on the save button.  
I create a support ticket to improve the experience.  
Let me know if it help.

---

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 15, 2021, 5:38pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/3 "2021-02-15T17:38:24Z")

</div>

Hi @Arnaud_Moncel

Unfortunately, that is not the case.

The field is not populated for creation, this is why I opened the ticket.

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

I 'm getting an error stating that the `addresses.user_id` field is null.

Thanks.

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [February 16, 2021, 2:42pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/4 "2021-02-16T14:42:25Z")

</div>

Hello @Louis-Marie,

Could you please try to change the `user_id` field referenced in your address model to `user.id` or simply `id`?

ie: `db.addresses.belongsTo(db.users, { as: 'user', foreignKey: 'user.id' });` or `db.addresses.belongsTo(db.users, { as: 'user', foreignKey: 'id' });`

If this does not work, would you mind sharing your user and address model files?

---

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 16, 2021, 2:58pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/5 "2021-02-16T14:58:34Z")

</div>

Hi @anon79585656

When trying to change the `user_id` field referenced in my address model, I 'm getting new errors, and it is not normal.

If I set

```javascript
db.addresses.belongsTo(db.users, { as: 'user', foreignKey: 'user.id' })

```

I’m getting

```javascript
SequelizeDatabaseError: column addresses.user.id does not exist

```

If I set

```javascript
db.addresses.belongsTo(db.users, { as: 'user', foreignKey: 'id' })

```

I’m getting no error (because the id field in addresses exist), but there is no more link from `addresses` to `users`.

My relation is correct: `foreign_key` should define the source key in `addresses`: it is user\_id.

Here is an excerpt of my `users` table:

```javascript
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('users',
    {
      id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        primaryKey: true,
      },
      created_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
        allowNull: false,
      },
      updated_at: {
        type: DataTypes.DATE,
        allowNull: true,
      },
      deleted_at: {
        type: DataTypes.DATE,
        allowNull: true,
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          isEmail: {
            args: true,
            msg: 'Please enter a valid email address.',
          },
          len: [10, 50],
        },
      },
      ...
      status: {
        type: DataTypes.INTEGER,
        allowNull: true,
        defaultValue: 1,
      },
    }, {
      tableName: 'vusers',
      timestamps: false,
      underscored: true,
      schema: process.env.DATABASE_SCHEMA,
    });

```

And an excerpt of my `addresses` table:

```javascript
module.exports = (sequelize, DataTypes) => {
  const Address = sequelize.define('addresses',
    {
      id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        primaryKey: true,
      },
      created_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
        allowNull: false,
      },
      updated_at: {
        type: DataTypes.DATE,
        allowNull: true,
      },
      deleted_at: {
        type: DataTypes.DATE,
        allowNull: true,
      },
      street: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      ....
      user_id: {
        type: DataTypes.UUIDV4,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
      },
    }, {
      tableName: 'addresses',
      timestamps: false,
      underscored: true,
    });

```

---

<div class="post-metadata">

**Author:** ![anon79585656](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@anon79585656](https://community.forestadmin.com/u/anon79585656)\
**Post date:** [February 16, 2021, 4:26pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/6 "2021-02-16T16:26:10Z")

</div>

Ok, too bad.

When defining the relations can you try to be explicit on the field to use?

```javascript
db.users.hasMany(db.addresses, { foreignKey: 'user_id' );

```

---

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 16, 2021, 4:44pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/7 "2021-02-16T16:44:43Z")

</div>

Hi @anon79585656

Great! that works now.

The difference I can see in the .forestadmin-schema.json is the following one in the `users` collection:

```javascript
- "reference": "addresses.userId",
+ "reference": "addresses.user_id",

```

So, do you know why it was wrongly generated, before specifying it in details?

I have a lot of hasMany relations, and I would like to avoid to change all of them 😉

Thanks.

---

<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:** [February 17, 2021, 10:30am UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/8 "2021-02-17T10:30:40Z")

</div>

Hi @Louis-Marie,

When you mean that it was wrongly generated, you mean in the `.forestadmin-schema.json`, right?  
Was this model manually created or it was generated via lumber?

---

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 17, 2021, 10:41am UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/9 "2021-02-17T10:41:20Z")

</div>

Hi @anon39848301

Yes, before specifying the `{ foreignKey: 'user_id' )` in the `db.users.hasMany` assiociation, the `reference` field of the `addresses` field in the `users` collection was wrongly generated in `.forestadmin-schema.json`.

The models were manually created.

Looking back at the different releases, I think that this wrong generation occurred since I migrated from sequelize v4 to sequelize v5.

But I have no sequelize problem, it occurs only in Forest.

Thanks

---

<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:** [February 17, 2021, 11:09am UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/10 "2021-02-17T11:09:48Z")

</div>

Thanks. It’ll be hard to track where the issue comes from. The `.forestadmin-schema.json` relies on `association.foreignKey`, provided by Sequelize. My guess is that Sequelize is able to retrieve the foreign key association is simple case like yours when the `foreignKey` value is not specified (By generating a value himself).

At the point where we generate the `.forestadmin-schema.json`, we highly relies on the Sequelize provided values.

The solution proposed by @anon79585656 seems to be the way to go from a Sequelize point of view ([Manual | Sequelize](https://sequelize.org/master/manual/naming-strategies.html)).

Looking at the sequelize `has-many`/`belongsTo` code, in case the foreign key is not provided, it generates it by himself ([sequelize/has-many.js at main · sequelize/sequelize · GitHub](https://github.com/sequelize/sequelize/blob/main/lib/associations/has-many.js#L65) and [sequelize/belongs-to.js at main · sequelize/sequelize · GitHub](https://github.com/sequelize/sequelize/blob/main/lib/associations/belongs-to.js#L41)).

Testing it on my end, not specifying any `foreignKey` attribute seems to work (Letting Sequelize handling the `foreignKey` value by himself for both `belongsTo` & `hasMany`), and also specifying the foreign key in both of those cases.

Not sure if that helps (since both cases would require code modification on your end), but let me know

---

<div class="post-metadata">

**Author:** ![Louis-Marie](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@Louis-Marie](https://community.forestadmin.com/u/Louis-Marie)\
**Post date:** [February 17, 2021, 5:28pm UTC](https://community.forestadmin.com/t/id-of-parent-collection-not-filled-in-when-creating-a-new-record/1846/11 "2021-02-17T17:28:11Z")

</div>

Hi @anon39848301

I have not found why it does not work as expected on my end, but Ok, I have updated the code now.  
So we can close this ticket.

Thanks for your help 🙂  
Best regards
