Can't save NOT NULL fields with empty strings

Hello,

We have some NOT NULL string fields with a ''::character varying default in our database.
When trying to update a record through ForestAdmin interface, those fields prevent us from saving with a “can’t be blank” message.

Is there a setting to allow those field to be saved with an empty string instead of failing the validation ?

Thanks !

Package Version:
Express Version: ~4.16.3
Sequelize Version: ^5.15.2, forest-express-sequelize: ^6.3.6
Database Dialect: Postgres
Database Version:
Project Name: Pennylane

Hi @jlb,

Welcome to the ForestAdmin community and thank you for sharing your issue.

In order to help you in the best way, would you mind sharing with me the model definition of the collection on which you are experiencing this issue?

You can find it under models/{your_model_name}.js

Thanks you

Here is a minimal definition:

module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  const TableName = sequelize.define('table_name', {
    columnName: {
      type: DataTypes.STRING,
      field: 'column_name',
      defaultValue: "",
      allowNull: false,
      set(val) {
        this.setDataValue('columnName', val || "")
      },
    },
  }, {
    tableName: 'table_name',
    schema: "public",
  });

  return TableName;
};

I’ve been able to reproduce the issue.

It comes from ForestAdmin’s way of handling null values and empty values the same way. I’ll make a product request to improve this point.

In the meanwhile, dropping (or commenting) the allowNull: false on your model’s definition should let you create and update records flawlessly. It’s not the perfect solution but it will let you work properly while the database will still be able to block null entries and populate the correct empty default value.

I hope it helped.

2 Likes

Thanks, it did the trick.

1 Like