sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

defaultValue for JSON(B) properties includes ", causing error

Open mariusa opened this issue 3 years ago • 2 comments

In Postgres, create a table with json default value

CREATE TABLE test(
id text not null,
props jsonb DEFAULT '{"optionA": 14, "optionB": "half"}'
);

Generated model is

import _sequelize from 'sequelize';
const { Model, Sequelize } = _sequelize;

export default class test extends Model {
  static init(sequelize, DataTypes) {
  return super.init({
    id: {
      type: DataTypes.TEXT,
      allowNull: false,
      primaryKey: true
    },
    props: {
      type: DataTypes.JSONB,
      allowNull: true,
      defaultValue: {\"optionA\": 14, \"optionB\": \"half\"}
    }
  }, {
    sequelize,
    tableName: 'test',
    schema: 'public',
    timestamps: false
  });
  }
}

causing node error

SyntaxError: Invalid or unexpected token
    at Loader.moduleStrategy (node:internal/modules/esm/translators:146:18)

"version": "0.8.8"

Thanks

mariusa avatar Jan 04 '22 09:01 mariusa

PS: the default value is already set in db. It would make sense to leave out that JSONB column on insert, if not specifically set in .create(). Changing default column values in db shouldn't require updating models.

mariusa avatar Jan 18 '22 18:01 mariusa

Just tested deleting defaultValue and everything works great. The best solution for this issue would be #592

mariusa avatar Jan 18 '22 18:01 mariusa