0

I have two models in Sequelize as below:

export const User = db.define('user', {
  id: {
    type: Sequelize.UUID,
    primaryKey: true,
  },
});

export const Class = db.define('class', {
  id: {
    type: Sequelize.UUID,
    primaryKey: true,
  },
  students: Sequelize.ARRAY({
    type: Sequelize.UUID,
    references: { model: 'users', key: 'id' },
  })
});

How can I define an association between my Class model and the user model?

I have tried the below but it gives me an error.

Class.hasMany(User, { foreignKey: 'students' });
User.belongsTo(Class);

DatabaseError [SequelizeDatabaseError]: column "class_id" does not exist

Vaulstein
  • 20,055
  • 8
  • 52
  • 73

1 Answers1

1

I think you are missing the syntax

students: Sequelize.ARRAY({
    type: Sequelize.UUID,
    references: { model: 'users', key: 'id' }, // this has no effect
})

Should be

students: {
    type: DataTypes.ARRAY({ type: DataTypes.UUID }),
    references: { model: 'users', key: 'id' }
}

This won't work either, because the data type of students (ARRAY) and id (UUID) of User does not match.

Also, with these associations, you are adding two columns on User referencing id of Class but you only need one

Class.hasMany(User, { foreignKey: 'students' }); //will add students attribute to User

User.belongsTo(Class); //will add classId attribute to User

if you want to name the foreign key column passe the same name to both associations, by default Sequelize will add classId, however if you configured underscored: true on the models it will be class_id

Here is a working solution

const User = sequelize.define('user', {
    id: { type: DataTypes.UUID, primaryKey: true },
});

const Class = sequelize.define('class', {
    id: { type: DataTypes.UUID, primaryKey: true },
    students: DataTypes.ARRAY({ type: DataTypes.UUID }),
});

Class.hasMany(User, { foreignKey: 'class_id' });
User.belongsTo(Class, { foreignKey: 'class_id' });
mousto090
  • 1,939
  • 1
  • 13
  • 12
  • This doesn't work for me. I don't understand how are you linking Class and User tables, students should be referenced to define the link. If I try this, it tries to search for class_id in User table – Vaulstein Jun 18 '20 at 06:12
  • This will create `class_id` column on `User` which refers to `id` of `Class`. Here is the SQL output `CREATE TABLE IF NOT EXISTS "classes" ("id" UUID , "students" UUID[], PRIMARY KEY ("id"))` `CREATE TABLE IF NOT EXISTS "users" ("id" UUID , "class_id" UUID REFERENCES "classes" ("id") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY ("id"))` – mousto090 Jun 18 '20 at 12:54
  • If you want to make `students` an array containing foreign keys to `User` `id`, it's not possible to define foreign key constraints on array elements, so you may not need associations between your models. see this link https://stackoverflow.com/questions/41054507/postgresql-array-of-elements-that-each-are-a-foreign-key – mousto090 Jun 18 '20 at 12:54
  • Thank you for this link, gives me clarity on the subject. – Vaulstein Jun 18 '20 at 16:54