adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Can't null out numeric field with postgres

Open Craig1f opened this issue 1 year ago • 0 comments

Describe the bug

When trying to use adminjs to clear the value of a nullable field of type "numeric", the value is not removed. Instead, it is set to 1 after reload.

Installed libraries and their versions

To Reproduce

  • Use postgres version 13.5
  • Set a field to the type: "numeric". Field is nullable.
  • Model this existing table with sequelize. Use DataTypes.Decimal
    • Note: The table wasn't built with sequelize because it's controlled by a different group in the project. Unclear if this is important
  • Set a value to this field
  • Using adminjs, attempt to delete the value by clearing it out in the input textbox
  • Value is not cleared out. Instead, upon reload, the value is 1
  • Looking under-the-hood, the request.payload.field has a value of empty string, not null
  • Fixed by adding a before-hook to change the empty string to a null

Expected behavior When clearing out the html text input for a Numeric field, I expect that field to be set to null

Screenshots Screenshots are not useful

AdminJSOptions with schema

The below before-hook fixes the problem

DatabaseName: {
  actions: {
    edit: {
      before: async (req) => {
        const modifiedReq = req;
        // Problem: Attempting to set these filds to "null" instead sets them to an empty string, which doesn't succesfully null out the column
        // Setting the value to null solves this
        modifiedReq.payload = {
          ...req.payload,
          latitude: req.payload.columnName=== '' ? null : req.payload.columnName
        };

        return modifiedReq;
      },
    },
  },
},

Desktop (please complete the following information if relevant): not relevant

Smartphone (please complete the following information if relevant): not relevant

Additional context No other relevant information

Craig1f avatar Oct 13 '22 20:10 Craig1f