sequelize / sequelize-auto

Automatically generate bare sequelize models from your database.
2.91k stars 529 forks source link

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

Open mariusa opened 2 years ago

mariusa commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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