简体   繁体   English

续集抛出错误“必须是唯一的”

[英]Sequelize throwing error "must be unique"

I am using Sequelize with my mysql database and have a many to many relationship with a CostumerDriverTransaction model as a through table我正在将 Sequelize 与我的 mysql 数据库一起使用,并且与作为直通表的 CostumerDriverTransaction 模型具有多对多关系

when I try to create a row form this table I get this error "message": "driverId must be unique"当我尝试从该表中创建一行时,我收到此错误"message": "driverId must be unique"

CostumerDriverTransaction Model顾客司机交易模型

const CostumerDriverTransaction = sequelize.define('CostumerDriverTransaction', {
costumerId: {
  type:DataTypes.INTEGER,
},
driverId: {
  type:DataTypes.INTEGER,
},
restaurantId: {
  type:DataTypes.INTEGER,
},
orderId: DataTypes.INTEGER,
transactionId: {
  type:DataTypes.INTEGER,
  primaryKey:true,
},
},{});

and this the association:这是协会:

index.js索引.js

db.user.belongsToMany(db.driver,{
  through:db.costumerDriverTransaction,
  foreignKey:{
    name:'costumerId'
  }
});
db.driver.belongsToMany(db.user,{
  through:db.costumerDriverTransaction,
  foreignKey:{
    name:'driverId'
  }
});
db.user.hasMany(db.costumerDriverTransaction,{
  foreignKey:{
    name:'costumerId'
  }
});
db.driver.hasMany(db.costumerDriverTransaction,{
  foreignKey:{
    name:'driverId'
  }
});
db.costumerDriverTransaction.belongsTo(db.user,{
  foreignKey:{
    name:'costumerId'
  }
});
db.costumerDriverTransaction.belongsTo(db.driver,{
  foreignKey:{
    name:'driverId'
  }
});

what's the problem please ?请问有什么问题吗?

As error indicates, you need to set driverId to be unique, that is:如错误所示,您需要将driverId设置为唯一,即:

const CostumerDriverTransaction = sequelize.define('CostumerDriverTransaction', {
costumerId: {
  type:DataTypes.INTEGER,
},
driverId: {
  type:DataTypes.INTEGER,
  unique: true
},
restaurantId: {
  type:DataTypes.INTEGER,
},
orderId: DataTypes.INTEGER,
transactionId: {
  type:DataTypes.INTEGER,
  primaryKey:true,
},
},{});

Most of the columns do not need to be specified in your define and will be create automatically, such at the primary keys and relationship fields.大多数列不需要在您的定义中指定并且会自动创建,例如在主键和关系字段中。 If you would like to override them, you can specify them in the column definition (this is only really notable because transactionId in your example would become id when autogenerated.如果您想覆盖它们,您可以在列定义中指定它们(这只是非常值得注意,因为您的示例中的transactionId在自动生成时将变为id

Here we create models for each of your objects and then define all the different relationships between them.在这里,我们为您的每个对象创建模型,然后定义它们之间的所有不同关系。 Because the relationship table is a "super" many-to-many by having it's own primary key instead of a composite you can query from it or any of the other models "through" it.因为关系表是“超级”多对多,因为它拥有自己的主键而不是组合,所以您可以从它或“通过”它的任何其他模型进行查询。

If you don't want the createdAt and updatedAt columns on a table pass timestamps: false, into the options for sequelize.define() .如果你不希望createdAtupdatedAt桌子上通栏timestamps: false,成选项sequelize.define()

// your other models
const User = sequelize.define('User', {}, {});
const Driver = sequelize.define('Driver', {}, {});
const Restaurant = sequelize.define('Restaurant', {}, {});
const Order = sequelize.define('Order', {}, {});

// the relational table, note that leaving off the primary key will use `id` instead of transactionId
const CostumerDriverTransaction = sequelize.define('CostumerDriverTransaction', {}, {});

// relate the transaction to the other 
CostumerDriverTransaction.belongsTo(User, { foreignKey: 'customerId' });
CostumerDriverTransaction.belongsTo(Driver, { foreignKey: 'driverId' });
CostumerDriverTransaction.belongsTo(Restaurant, { foreignKey: 'restaurantId' });
CostumerDriverTransaction.belongsTo(Order, { foreignKey: 'orderId' });

// relate the models to the transactions
User.hasMany(CostumerDriverTransaction, { as: 'transactions', foreignKey: 'customerId' });
// relate models to other models through transactions
User.hasMany(Driver, { as: 'drivers', through: 'CostumerDriverTransaction', foreignKey: 'customerId', otherKey: 'driverId' });
User.hasMany(Restaurant, { as: 'restaurants', through: 'CostumerDriverTransaction', foreignKey: 'customerId', otherKey: 'restaurantId' });
User.hasMany(Order, { as: 'orders', through: 'CostumerDriverTransaction', foreignKey: 'customerId', otherKey: 'orderId' });

// Drivers
Driver.hasMany(CostumerDriverTransaction, { foreignKey: 'driverId' });
Driver.hasMany(User, { as: 'customers', through: 'CostumerDriverTransaction', foreignKey: 'driverId', otherKey: 'customerId' });
Driver.hasMany(Restaurant, { as: 'restaurants', through: 'CostumerDriverTransaction', foreignKey: 'driverId', otherKey: 'restaurantId' });
Driver.hasMany(Order, { as: 'orders', through: 'CostumerDriverTransaction', foreignKey: 'driverId', otherKey: 'orderId' });

// Restaurants
Restaurant.hasMany(CostumerDriverTransaction, { foreignKey: 'restaurantId' });
Restaurant.hasMany(Driver, { as: 'drivers', through: 'CostumerDriverTransaction', foreignKey: 'restaurantId', otherKey: 'driverId' });
Restaurant.hasMany(User, { as: 'customers', through: 'CostumerDriverTransaction', foreignKey: 'restaurantId', otherKey: 'customerId' });
Restaurant.hasMany(Order, { as: 'orders', through: 'CostumerDriverTransaction', foreignKey: 'restaurantId', otherKey: 'orderId' });

// Orders
Order.hasMany(CostumerDriverTransaction, { foreignKey: 'orderId' });
Order.hasMany(Driver, { as: 'drivers', through: 'CostumerDriverTransaction', foreignKey: 'orderId', otherKey: 'driverId' });
Order.hasMany(User, { as: 'customers', through: 'CostumerDriverTransaction', foreignKey: 'orderId', otherKey: 'customerId' });
Order.hasMany(Restaurant, { as: 'restaurants', through: 'CostumerDriverTransaction', foreignKey: 'orderId', otherKey: 'restaurantId' });

This will create models with primary key and relationship columns of:这将创建具有主键和关系列的模型:

// User
{
  id: primary key,
  createdAt: create date,
  updatedAt: update date,
}

// Driver
{
  id: primary key,
  createdAt: create date,
  updatedAt: update date,
}

// Restaurant
{
  id: primary key,
  createdAt: create date,
  updatedAt: update date,
}

// Order
{
  id: primary key,
  createdAt: create date,
  updatedAt: update date,
}

// Transaction
{
  id: primary key,
  userId: relationship to User,
  driverId: relationship to Driver,
  restaurantId: relationship to Restaurant,
  orderId: relationship to Order,
  createdAt: create date,
  updatedAt: update date,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM