簡體   English   中英

續集拋出錯誤“必須是唯一的”

[英]Sequelize throwing error "must be unique"

我正在將 Sequelize 與我的 mysql 數據庫一起使用,並且與作為直通表的 CostumerDriverTransaction 模型具有多對多關系

當我嘗試從該表中創建一行時,我收到此錯誤"message": "driverId must be unique"

顧客司機交易模型

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

這是協會:

索引.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'
  }
});

請問有什么問題嗎?

如錯誤所示,您需要將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,
},
},{});

大多數列不需要在您的定義中指定並且會自動創建,例如在主鍵和關系字段中。 如果您想覆蓋它們,您可以在列定義中指定它們(這只是非常值得注意,因為您的示例中的transactionId在自動生成時將變為id

在這里,我們為您的每個對象創建模型,然后定義它們之間的所有不同關系。 因為關系表是“超級”多對多,因為它擁有自己的主鍵而不是組合,所以您可以從它或“通過”它的任何其他模型進行查詢。

如果你不希望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' });

這將創建具有主鍵和關系列的模型:

// 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