"BelongTo Typeahead" does not create related entity

Feature(s) impacted

Creation of an entity, with relation managed as “BelongTo Typeahead”

Observed behavior

Let’s say that I have a collection “User”, with a required field badgeId refering to another collection “Badge”.

When I create a User, I try to benefit from the feature “Create a new Badge” to create this collection on the go.

However, after filling Badge’s info, and clicking on “Create User”, my backend is sending me an error “badgeId” is required.
Indeed, no single data about the badge is send in the http query payload (data.relationships is not defined):

{
  "data": {
    "attributes": {
      "someField":...
    },
    "type":"user"
  }
}

Expected behavior

The call should contain data about User & Badge so that both are created by my backend on save.

Failure Logs

The server response:
{“errors”:[{“status”:500,“detail”:“null value in column "badgeId" of relation "badge" violates not-null constraint”,“name”:“SequelizeDatabaseError”}]}

Context

Postgresql, nodejs

  • Project name: …
  • Team name: …
  • Environment name: all
  • Agent technology: nodejs
  • Agent (forest package) name & version: forest-express-sequelize@9.3.25
  • Database type: postgresql
  • Recent changes made on your end if any: none

Basic declaration:

// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const User = sequelize.define(
    'user',
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
        defaultValue: Sequelize.literal('uuid_generate_v4()'),
        allowNull: false,
      },
      ...
    },
    {
      tableName: 'user',
      timestamps: false,
      schema: process.env.DATABASE_SCHEMA,
    }
  );

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-relationships.
  User.associate = models => {
    User.belongsTo(models.badge, {
      foreignKey: {
        name: 'badgeIdKey',
        field: 'badgeId',
      },
      as: 'badge',
    });
  };

  return User;
};

and

// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Badge = sequelize.define(
    'badge',
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
        defaultValue: Sequelize.literal('uuid_generate_v4()'),
        allowNull: false,
      },
      optionalField1: {
        type: DataTypes.STRING,
      },
      optionalField2: {
        type: DataTypes.STRING,
      },
    },
    {
      tableName: 'badge',
      timestamps: false,
      schema: process.env.DATABASE_SCHEMA,
    }
  );

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-relationships.
  Badge.associate = models => {};

  return Badge;
};

I notice that every single “BelongTo Typeahead” field in my 170+ collections’ project is broken, although perfectly functionning for years

1 Like

Sql schema is as simple as:

CREATE TABLE "badge" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "optionalField1" character varying(255) NOT NULL, "optionalField2" character varying(255) NOT NULL CONSTRAINT "PK_0" PRIMARY KEY ("id"))

CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "badgeId" uuid NOT NULL, ..., CONSTRAINT "PK_1" PRIMARY KEY ("id"));
ALTER TABLE "user" ADD CONSTRAINT "FK_2" FOREIGN KEY ("badgeId") REFERENCES "badge"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

Also everything works well if I first create a Badge, then go to User and create a User referring the existing Badge

Hello @momolili and thanks for your report.
I do indeed reproduce the issue. I have opened up an internal resolution ticket to resolve it.

I will keep this thread updated with the resolution, thanks for your patience :pray:

@Nicolas.M

Hi @momolili,

Could you give it a try ? We released a fix that should fix your issue :smile:

2 Likes

Works like a charm

Thanks for the quick fix

2 Likes

Hello @vince @Nicolas.M,

It appears the problem is now back. Since today or some day ago. Can you look into it please?

Thank you

Hello @louisl,

I tried reproducing the issue, but couldn’t on my side. It might be a different one.
Could you please create a new forum post with details about your particular setup, following the template below:

Feature(s) impacted

Observed behavior

Expected behavior

Failure Logs

Context

  • Project name: …
  • Team name: …
  • Environment name: …
  • Agent technology: (nodejs, php, rails, python)
  • Agent (forest package) name & version: …
  • Database type: …
  • Recent changes made on your end if any: …

This will help us with the investigation :pray:

Hello, thanks. Looks like I can’t reproduce with a simple case anymore and I don’t remember the specific case where it failed, I’ll get more info from the admin users who encountered the problem and create a new issue if needed.