解决sequelize在belongsToMany关联中的唯一约束 [英] Work around Sequelize’s unique constraints in belongsToMany associations

查看:87
本文介绍了解决sequelize在belongsToMany关联中的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了Sequelize.这是两个模型:

I'm using Sequelize in my project. These are the two models:

const User = db.define('user', {
  name: Sequelize.STRING,
  password: Sequelize.STRING
})
const Product = db.define('product', {
  name: Sequelize.STRING,
  price: Sequelize.INTEGER
})

现在用户可以购买产品了,我的关联设置如下:

Now users can purchase products and I have associations setup like below:

Product.belongsToMany(User, {through: 'UserProducts'})
User.belongsToMany(Product, {through: 'UserProducts'})

我也有此UserProducts表,其中有一个附加列.

I also have this UserProducts table with an additional column.

const UserProducts = db.define('UserProducts', {
  status: Sequelize.STRING
})

Sequelize创建一个包含userId和productId组合的组合键,并且只允许包含userId和productId组合的一条记录.因此,例如,userId 2和productId14.

Sequelize creates a composite key with combination of userId and productId and will only allow one record with a combination of userId and productId. So, for example, userId 2 and productId 14.

这对我来说是个问题,因为有时人们想多次购买.我需要以下方案之一才能工作:

This is a problem for me because sometimes people want to purchase multiple times. I need one of the following scenarios to work:

  1. 不要使用复合键,而是将一个全新的自动增量列用作UserProducts中的键.

  1. Don't use the composite key and instead have a completely new auto-increment column used as key in UserProducts.

不是让我仅使用userId和productId来创建密钥,而是让我在密钥中添加另外一列,例如 status ,以便唯一密钥是这三者的组合.p>

Instead of making key with userId and productId alone, allow me to add one more column into the key such as the status so that unique key is a combination of the three.

我确实想使用关联,因为它们提供了许多强大的方法,但我想更改唯一键以使其能够以用户ID和产品ID相同的组合添加多行的方式来工作.

I do want to use the associations as they provide many powerful methods, but want to alter the unique key to work in such a way that I can add multiple rows with the same combination of user id and product id.

由于我的模型/数据库已经在运行,所以我需要利用迁移来进行此更改.

And since my models/database is already running, I will need to make use of migrations to make this change.

对此有任何帮助,我们深表感谢.

Any help around this is highly appreciated.

推荐答案

当模型中不存在主键时,Belongs-To-Many将创建一个唯一键.

Belongs-To-Many creates a unique key when the primary key is not present on through model.

由于在 UserProducts 表中还具有其他属性,因此需要为 UserProducts 定义模型,然后告诉续集使用该模型而不是创建自己的模型

Since you also have additional property in your UserProducts table, you need to define the model for UserProducts and then tell the sequelize to use that model instead of creating its own

class User extends Model {}
User.init({
    name: Sequelize.STRING,
    password: Sequelize.STRING
}, { sequelize })

class Product extends Model {}
ProjProductect.init({
    name: Sequelize.STRING,
    price: Sequelize.INTEGER
}, { sequelize })

class UserProducts extends Model {}
UserProducts.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  status: DataTypes.STRING
}, { sequelize })

User.belongsToMany(Project, { through: UserProducts })
Product.belongsToMany(User, { through: UserProducts })

引荐:将v4序列化归属于ManyToMany

更新

由于您使用的是v3,并且已经为您的 UserProducts 表创建了模型,因此请使用以下代码段

since you are using v3 and already have a model created for your UserProducts table use following snippet

UserProducts = db.define('UserProducts', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  status: DataTypes.STRING
})

这篇关于解决sequelize在belongsToMany关联中的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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