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

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:

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

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

Hi @Louis-Marie :wave: 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.

Hi @Arnaud_Moncel

Unfortunately, that is not the case.

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

image

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

Thanks.

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?

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

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

I’m getting

SequelizeDatabaseError: column addresses.user.id does not exist

If I set

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:

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:

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,
    });

Ok, too bad.

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

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

Hi @anon79585656

Great! that works now.

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

-      "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 :wink:

Thanks.

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?

Hi @jeffladiray

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

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

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 and sequelize/belongs-to.js at main · sequelize/sequelize · GitHub).

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

Hi @jeffladiray

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 :slight_smile:
Best regards