4

I am new to NodeJs development I am using NodeJs with mysql and Sequelize to create a Batch model with these properties.

const Batch = sequelize.define(
  "Batch",
  {
    title: { type: DataTypes.STRING, allowNull: false },
    studentIds: { type: DataTypes.STRING },
    teacherId: { type: DataTypes.STRING, allowNull: true }
  },
  {
    timestamps: false
  }
);

On async method call it is working fine.

Batch.sync().then((res) => {
  console.log("Batch model sync : ", Batch === sequelize.models.Batch);
});

But I need to change

studentIds: { type: DataTypes.ARRAY(DataTypes.STRING)}

Whenever I make this change it gives error

enter image description here

I am using node 14.5.0 MySql 8.0.21 and Sequelize 6.3.4

2 Answers 2

8

DataTypes.ARRAY is not available on Mysql, it's only available on postgres.

Check in official docs: https://sequelize.org/api/v6/class/src/data-types.js~array

Sign up to request clarification or add additional context in comments.

1 Comment

so on mysql , it should be what when I need array
0

With a small workaround you can use your string data as an array. Overwrite the getter and setter of the property of the model

studentIds: {
    type: Sequelize.STRING,
    get() {
      const stringValue = this.getDataValue('studentIds');
      return stringValue ? rawValue.split(',') : null;
    },
    set(value) {
      const arrayValue = value ? value.join(',') : '';
      this.setDataValue('studentIds', arrayValue);
    },
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.