[英]sequelize eager loading returns null for associated table record. Why?
我想從關聯表中檢索數據,但返回值為空。 這是協會
static associate(models) {
appointment.hasOne(models.member, {
foreignKey: "id",
sourceKey: "member_id",
constraints: false,
});
appointment.hasOne(models.system_data, {
foreignKey: "id",
sourceKey: "facility_id",
constraints: false,
as: "system_data",
});
}
成員關聯已正確返回,但是當我嘗試獲取 system_data 時,即使它存在於數據庫中,我也會得到 null。 在這里我嘗試得到:
const getRelatedTableRecords = () =>
include?.split(",").map((table) => {
if (schemaName === "appointment" && table === "system_data") {
return { model: db[table], as: "system_data", required: false };
}
return { model: db[table] };
});
我不明白為什么我無法獲取 system_data。 可能是什么原因?? 你有什么建議嗎?
成員對象
class Member extends Model {
static associate(models) {
// define association here
Member.hasMany(models.card, {
foreignKey: "card_id",
sourceKey: "card_id",
});
Member.hasMany(models.club, {
foreignKey: "id",
sourceKey: "club",
constraints: false,
});
Member.hasMany(models.schedule, {
foreignKey: "id",
sourceKey: "schedule",
constraints: false,
});
Member.hasMany(models.trainer, {
foreignKey: "id",
sourceKey: "trainer",
constraints: false,
});
Member.hasMany(models.file, {
foreignKey: "owner_id",
sourceKey: "id",
constraints: false,
});
Member.hasOne(models.facilities, {
foreignKey: "id",
sourceKey: "facility_id",
constraints: false,
});
}
}
Member.init(
{
first_name: {
type: DataTypes.STRING,
allowNull: false,
},
last_name: {
type: DataTypes.STRING,
allowNull: false,
},
tc_no: {
type: DataTypes.BIGINT,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
},
card_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
allowNull: false,
},
registered: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
schedule: {
type: DataTypes.INTEGER,
references: {
model: "schedules",
key: "id",
},
onDelete: "CASCADE",
},
club: {
type: DataTypes.INTEGER,
references: {
model: "clubs",
key: "id",
},
onDelete: "CASCADE",
allowNull: true,
},
trainer: {
allowNull: true,
type: DataTypes.INTEGER,
references: {
model: "trainers",
key: "id",
},
onDelete: "CASCADE",
},
birthplace: {
type: DataTypes.STRING,
allowNull: true,
},
birthdate: { type: DataTypes.DATE, allowNull: true },
father_name: { type: DataTypes.STRING, allowNull: true },
mother_name: { type: DataTypes.STRING, allowNull: true },
gender: { type: DataTypes.STRING, allowNull: true },
profession: { type: DataTypes.STRING, allowNull: true },
address: { type: DataTypes.STRING, allowNull: true },
phone_number: { type: DataTypes.STRING, allowNull: true },
hes_code: { type: DataTypes.STRING, allowNull: true },
blood_type: { type: DataTypes.STRING, allowNull: true },
nationality: { type: DataTypes.STRING, allowNull: true },
profile_photo: {
type: DataTypes.STRING,
allowNull: true,
},
file: {
type: DataTypes.STRING,
allowNull: true,
},
session_ids: { type: DataTypes.ARRAY(DataTypes.STRING) },
facility_id: { type: DataTypes.INTEGER },
following: { type: DataTypes.ARRAY(DataTypes.INTEGER) },
creator: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
updater: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0,
},
},
{
sequelize,
modelName: "member",
hooks: {
afterCreate: (member, options) => {
sequelize.models.card.create({
card_id: member.card_id,
credits: 0,
creator: member.creator,
updater: member.updater,
});
},
beforeUpdate: (member, options) => {
const changed = member.changed();
if (changed?.includes("card")) {
sequelize.models.member.update(
{ trainer: null },
{ where: { id: member.dataValues.id } }
);
}
if (changed?.includes("trainer")) {
sequelize.models.member.update(
{ club: null },
{ where: { id: member.dataValues.id } }
);
}
},
},
}
);
系統數據對象
class System_data extends Model {
static associate(models) {
// define association here
System_data.hasOne(models.facilities, {
foreignKey: "id",
sourceKey: "facility_id",
constraints: false,
});
}
}
System_data.init(
{
app_id: { type: DataTypes.STRING },
province: DataTypes.STRING,
district: DataTypes.STRING,
address: DataTypes.TEXT,
phone: DataTypes.STRING,
reset_credits: { type: DataTypes.BOOLEAN, defaultValue: false },
asset_path: {
type: DataTypes.STRING,
},
card_price: {
type: DataTypes.DECIMAL,
allowNull: false,
defaultValue: 0.0,
},
ticket_price: {
type: DataTypes.DECIMAL,
allowNull: false,
defaultValue: 0.0,
},
facility_id: { type: DataTypes.INTEGER },
working_hours: { type: DataTypes.STRING },
capacity: DataTypes.BIGINT,
season_based: { type: DataTypes.BOOLEAN, defaultValue: false },
appointment_based: { type: DataTypes.BOOLEAN, defaultValue: false },
seasons: {
type: DataTypes.ARRAY(DataTypes.STRING),
},
season_capacity: {
type: DataTypes.INTEGER,
},
},
{
sequelize,
modelName: "system_data",
}
);
正如我所看到的system_data
有facility_id
作為appointment
的外鍵,所以appointment
應該是這樣的:
appointment.hasOne(models.system_data, {
foreignKey: "facility_id",
constraints: false,
as: "system_data",
});
在這對hasOne/belongsTo
的兩個關聯中,您應該使用相同的值指示相同的foreignKey
選項,該選項應該指向N
側表中1:N
關系中的外鍵字段。
只要使用1:N
關系的1
側表的主鍵字段,就不需要指明sourceKey
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.