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