Skip to content

Exclude id attribute in model if using PostgreSQL? #9

@athenawisdoms

Description

@athenawisdoms

Hello, I am new to PostgreSQL and sequelize so the following might be the intended behavior and not a real issue.

sequelize-automate generates model files that include the id attribute when the dialect chosen is postgres, such as:

const {
  DataTypes
} = require('sequelize');

module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: "nextval(my_data_id_seq::regclass)",
      comment: null,
      primaryKey: false,
      field: "id"
    },
    score: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: null,
      primaryKey: false,
      field: "score"
    },
  };
  const options = {
    tableName: "my_data",
    comment: "",
    indexes: []
  };
  const MyDataModel = sequelize.define("my_data_model", attributes, options);
  return MyDataModel;
};

When inserting a row to the my_data table using my_data_model.insert(payload), we get the error:

Unhandled rejection SequelizeDatabaseError: invalid input syntax for integer: "nextval(my_data_id_seq::regclass)"

Some researching of similar issues show that

  1. the id column does not need to be defined in the sequelize model , because ...
  2. PostgreSQL will automatically handle the auto-incrementing id column if it exists.

Manually removing the id attribute from the model generated by sequelize-automate appears to get everything working as expected, with sequelize/PostgreSQL automatically populating the id column with an auto-incrementing integer.

Question: If my understanding is correct, sequelize-automate should exclude the id attribute in the models that it generates if using PostgreSQL. Is there an option to do this? Or should sequelize-automate do this automatically?

Thanks again!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions