如何在Sequelize中配置一对多关系? [英] How to configure a one to many relationship in Sequelize?

查看:174
本文介绍了如何在Sequelize中配置一对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将Express与Sequelize + MySQL一起使用,并且想知道对我来说解决此问题的最佳方法是什么.对于这是一个基本问题,我深表歉意,因为我对Sequelize甚至是一般的SQL数据库还很陌生.

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.

我有这样的模型User

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,
  },
});

然后我也有另一个模型Shoe

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);

但是,这样做的问题是它创造"了鞋子,这导致了重复.我基本上想要一个名为"Shoes"的表,其中包含鞋子,然后用户只需根据其拥有的鞋子来引用ID.

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.

不确定执行此操作的最佳方法是什么,尤其是如果用户拥有多双鞋子.用伪代码,我想我想要类似鞋子ID的数组,例如shoes: [1, 2, 3, 4],当被查询时,便以某种方式在shoe表中查找并插入到User响应中.

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.

很明显,我可以在原始SQL中执行类似的操作,但是我认为,鉴于Sequelize具有强大的功能,必须有一种更好的方法来解决此问题.

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!

推荐答案

  1. 我建议使用tableName选项.
  2. 如果要存储用户",鞋子"以及每个用户拥有的鞋子的信息,则需要创建第三个表.这是n:m关联,因为每个用户可能有几双鞋子,而每个鞋子可能属于几个用户.您可以通过以下几种方法创建该表:

  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:

User.belongsToMany(Shoe, {through: 'UserShoe'});
Shoe.belongsToMany(User, {through: 'UserShoe'});

  • 定义UserShoe模型,然后关联User,Shoe和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);
    

  • 这篇关于如何在Sequelize中配置一对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆