gpt4 book ai didi

node.js - 保存 Sequelize 记录/在另一个模型中引用它

转载 作者:行者123 更新时间:2023-12-03 22:36:19 25 4
gpt4 key购买 nike

我有一个名为 Segment 的 postrgresql/Sequelize 模型,它属于许多模型:

module.exports = (sequelize, DataTypes) => {
const Segment = sequelize.define(
'segment',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
provider_id: {
type: DataTypes.INTEGER,
references: {
model: 'provider',
key: 'id'
}
},
summary_id: {
type: DataTypes.INTEGER,
references: {
model: 'summary',
key: 'id'
}
},
audience_id: {
type: DataTypes.INTEGER,
references: {
model: 'audience',
key: 'id'
}
},
onboarding_id: {
type: DataTypes.INTEGER,
references: {
model: 'onboarding',
key: 'id'
}
}
},
{
// disable the modification of table names; By default, sequelize will automatically
// transform all passed model names (first parameter of define) into plural.
// if you don't want that, set the following
freezeTableName: true,
tableName: 'segment'
}
);
Segment.associate = models => {
Segment.belongsTo(models.Provider, { foreignKey: 'id' });
Segment.belongsTo(models.Summary, { foreignKey: 'id' });
Segment.belongsTo(models.Audience, { foreignKey: 'id' });
Segment.belongsTo(models.Onboarding, { foreignKey: 'id' });
};
return Segment;
};

分段具有关联的模型(即 provider_idsummary_idaudience_idonboarding_id )如下所示:

供应商:
module.exports = (sequelize, DataTypes) => {
const Provider = sequelize.define(
'provider',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
providerName: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING
},
privacyPolicy: {
type: DataTypes.STRING
}
},
{
freezeTableName: true,
tableName: 'provider'
}
);

Provider.associate = models => {
Provider.hasMany(models.Segment, { foreignKey: 'provider_id' });
};

return Provider;
};


概括:
module.exports = (sequelize, DataTypes) => {
const Summary = sequelize.define(
'summary',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
summaryName: DataTypes.STRING,
standardIdName: DataTypes.STRING,
description: DataTypes.STRING,
},
{
freezeTableName: true,
tableName: 'summary'
}
);

Summary.associate = models => {
Summary.hasMany(models.Segment, { foreignKey: 'summary_id' });
};

return Summary;
};


观众:
module.exports = (sequelize, DataTypes) => {
const Audience = sequelize.define(
'audience',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
refreshCadence: DataTypes.STRING,
sourceLookbackWindow: DataTypes.STRING
},
{
freezeTableName: true,
tableName: 'audience'
}
);

Audience.associate = models => {
Audience.hasMany(models.Segment, { foreignKey: 'audience_id' });
};

return Audience;
};

入职:
module.exports = (sequelize, DataTypes) => {
const Onboarding = sequelize.define(
'onboarding',
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
onboardingExpansions: DataTypes.STRING,
onboardingAudiencePrecision: DataTypes.STRING
},
{
freezeTableName: true,
tableName: 'onboarding'
}
);

Onboarding.associate = models => {
Onboarding.hasMany(models.Segment, { foreignKey: 'onboarding_id' });
};

return Onboarding;
};


我的问题是:创建和保存 Segment 记录时应该先做什么?我是否首先创建并保存其他模型中的每一个(提供者、摘要、受众、入职),然后创建/保存引用这些 ID 的分割?我真的不知道在这种情况下事件的顺序应该是什么。任何帮助深表感谢!谢谢!

最佳答案

  • TLDR:

  • 为了创建 Segment 的实例,您必须在被引用的表( providersummaryaudienceonboarding )上拥有所有 4 个外键引用存在记录。
  • 解释:
  • providersummaryaudienceonboarding 表是独立的。

    然而, Segment 模型是 而不是 独立的。 Segment 模型有 4 列是外键。

    From PostgresSql Tutorial:

    A foreign key is a field or group of fields in a table that uniquely identifies a row in another table. In other words, a foreign key is defined in a table that references to the primary key of the other table.

    The table that contains the foreign key is called referencing table or child table. And the table to which the foreign key references is called referenced table or parent table.



    这意味着外键是一个约束,列应该引用被引用表的主键。

    因此,您必须创建已创建的 Segment 行的所有资源。

    关于node.js - 保存 Sequelize 记录/在另一个模型中引用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59973861/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com