我有 3 张桌子:
rate
、paymentOption
和 paymentOptionRate
。当我尝试在 bulkCreate
上执行 paymentOptionRate
时,我传递了一个像这样的数组:
const data = [
{
rateId: ccf19a9f-9897-4328-84ba-f2558fbbe10a,
optionRateId: 9f873f92-7fbd-492e-9416-dd12f7a71dee
},
{
rateId: 4329965a-89d0-46da-83f3-fbea27da8206,
optionRateId: fae5edcc-f03e-4744-ba52-0da73406640c
},
]
但是由于某种原因我收到了这个错误:
ERROR: column "RateId" of relation "PaymentOptionRates" does not exist
这是我的模型:
rate
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Rate extends Model {
static associate(models) {}
}
Rate.init({
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
rate: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false,
},
subsidiaryId: {
type: DataTypes.UUID,
references: {
model: 'Subsidiaries',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
}, {
sequelize,
modelName: 'Rate',
timestamps: true,
});
Rate.associate(models => {
Rate.belongsTo(models.Subsidiary, {
foreignKey: 'subsidiaryId',
as: 'subsidiary',
});
Rate.belongsToMany(models.PaymentOption, {
through: models.PaymentOptionRate,
foreignKey: 'rateId',
as: 'paymentOptions',
});
});
return Rate;
};
paymentOption
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class PaymentOption extends Model {
static associate(models) {}
}
PaymentOption.init({
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
sequelize,
modelName: 'PaymentOption',
timestamps: true,
});
PaymentOption.associate = function(models) {
PaymentOption.belongsToMany(models.Rate, {
through: models.PaymentOptionRate,
foreignKey: 'paymentOptionId',
as: 'rates',
});
};
return PaymentOption;
};
paymentOptionRate
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class PaymentOptionRate extends Model {
static associate(models) {}
}
PaymentOptionRate.init({
paymentOptionId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'PaymentOptions',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
rateId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'Rates',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
}, {
sequelize,
modelName: 'PaymentOptionRate',
timestamps: true,
});
PaymentOptionRate.associate = function(models) {
PaymentOptionRate.belongsTo(models.PaymentOption, {
through: models.PaymentOptionRate,
foreignKey: 'paymentOptionId',
as: 'paymentOption',
});
PaymentOptionRate.belongsTo(models.Rate, {
through: models.PaymentOptionRate,
foreignKey: 'rateId',
as: 'rate',
});
};
return PaymentOptionRate;
};
我也执行了这个命令:
console.log(await PaymentOptionRate.describe());
结果是:
{
paymentOptionId: {
type: 'UUID',
allowNull: false,
defaultValue: null,
comment: null,
special: [],
primaryKey: false
},
rateId: {
type: 'UUID',
allowNull: false,
defaultValue: null,
comment: null,
special: [],
primaryKey: false
},
createdAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
comment: null,
special: [],
primaryKey: false
},
updatedAt: {
type: 'TIMESTAMP WITH TIME ZONE',
allowNull: false,
defaultValue: null,
comment: null,
special: [],
primaryKey: false
}
}
一切看起来都不错,但由于某种原因我也遇到了这个错误:
sql: 'INSERT INTO "PaymentOptionRates" ("paymentOptionId","rateId","createdAt","updatedAt","RateId") VALUES ($1,$2,$3,$4,$5) RETURNING "paymentOptionId","rateId","createdAt","updatedAt","RateId";',
parameters: [
'3f5a8209-b5ae-47b1-b838-84d3dc563ecc',
'8121ce6e-a2aa-42a0-b2a9-186227af3720',
'2023-11-25 07:50:01.187 +00:00',
'2023-11-25 07:50:01.187 +00:00',
null
]
我已经用其他表执行了其他
bulkCreates
,但只是使用belongsTo
关联并正确执行,但在这种情况下是manyToMany
关系,我遇到了这个问题。
如果您使用非默认外键列名称,那么您需要在all相关关联中指出它们:
Rate.belongsToMany(models.PaymentOption, {
through: models.PaymentOptionRate,
foreignKey: 'rateId',
otherKey: 'paymentOptionId',
as: 'paymentOptions',
});
PaymentOption.belongsToMany(models.Rate, {
through: models.PaymentOptionRate,
foreignKey: 'paymentOptionId',
otherKey: 'rateId',
as: 'rates',
});