user5113508
user5113508

Reputation:

How to configure a one to many relationship in Sequelize?

I'm currently using Express with Sequelize + MySQL and was wondering what the best approach for me to solve this problem was. I apologise if this is a basic question as I'm pretty new to Sequelize and even SQL databases in general.

I have a model User like so;

export default db.define('User', {
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true,
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  email: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  createdAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.NOW,
  },
  updatedAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.NOW,
  },
});

Then I also have another model Shoe like so;

export default db.define('Shoe', {
  id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true,
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  size: {
    type: Sequelize.INTEGER,
    allowNull: true,
  },
  quality: {
    type: Sequelize.ENUM('Brand New', 'Used', 'Collector Item'),
    allowNull: false,
  },
  createdAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.NOW,
  },
  updatedAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.NOW,
  },
});

I then define an association between these two.

User.hasMany(Shoe);
Shoe.belongsTo(User);

However, the problem with this is that it 'creates' the shoe and this leads to duplication. I basically want a table called "Shoes" that has the shoes in it, and then the user just references the ids based on what shoes it has.

Not sure what the best way to do this is, especially if a user owns multiple shoes. In pseudo I suppose I'd want something like an array of shoe id's such as shoes: [1, 2, 3, 4] that when queried then get looked up in the shoes table somehow and inserted into the User response.

Obviously I can do something like that in raw SQL, but I figure there must be a better way to approach this given how much power Sequelize has.

Hopefully someone can provide some advice and help regarding this!

Upvotes: 2

Views: 2379

Answers (1)

Faris
Faris

Reputation: 241

  1. I recommend use tableName option.
  2. If you want store Users, Shoes and info about what shoe each user have, you need create third table. This n:m association, because each user may have several shoes, and each shoe may belongs to several users. You can create this table by several ways:

    • see Model.belongsToMany tutorial and api doc

      User.belongsToMany(Shoe, {through: 'UserShoe'});
      Shoe.belongsToMany(User, {through: 'UserShoe'});
      
    • define UserShoe model and then associate User, Shoe and UserShoe models:

      export default db.define('UserShoe', {
        userId: {
          type: Sequelize.INTEGER,
          allowNull: false,
          primaryKey: true
        },
        shoeId: {
          type: Sequelize.INTEGER,
          allowNull: false,
          primaryKey: true
        }
      });
      
      
      User.belongsToMany(Shoe, {through: UserShoe});
      Shoe.belongsToMany(User, {through: UserShoe});
      
    • or like this:

      User.hasMany(UserShoe);
      UserShoe.belongsTo(User);
      Shoe.hasMany(UserShoe);
      UserShoe.belongsTo(Shoe);
      
  3. I recommend you read a book SQL antipatterns / Karwin Bill. Raleigh and Sequelize association docs.

Upvotes: 3

Related Questions