"there is no unique constraint matching given keys for referenced table "配置文件" "创建多对多关联



我正在尝试使用typescript和sequelize来创建Profile模型和Housing模型之间的多对多关系。一个配置文件有许多与之相关的住房。一个给定的住房条目可能有许多与之相关的配置文件。我想我已经很接近了,但这是我第一次做这种东西。因此我得到错误

Database connection failed Error
at Query.run (/home/rlm/Code/apGymBE/node_modules/sequelize/src/dialects/postgres/query.js:76:25)
at /home/rlm/Code/apGymBE/node_modules/sequelize/src/sequelize.js:641:28
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PostgresQueryInterface.addColumn (/home/rlm/Code/apGymBE/node_modules/sequelize/src/dialects/abstract/query-interface.js:430:12)
at async Function.sync (/home/rlm/Code/apGymBE/node_modules/sequelize/src/model.js:1373:11)
at async Sequelize._syncModelsWithCyclicReferences (/home/rlm/Code/apGymBE/node_modules/sequelize/src/sequelize.js:847:7) {
name: 'SequelizeDatabaseError',
parent: error: there is no unique constraint matching given keys for referenced table "Profiles"
at Parser.parseErrorMessage (/home/rlm/Code/apGymBE/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/home/rlm/Code/apGymBE/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/home/rlm/Code/apGymBE/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/rlm/Code/apGymBE/node_modules/pg-protocol/src/index.ts:7:48)
// later in the error
sql: 'ALTER TABLE "public"."Profile_Housings" ADD COLUMN "ProfileProfileId" INTEGER  REFERENCES "Profiles" ("profileId") ON DELETE CASCADE ON UPDATE CASCADE;',
parameters: undefined
},
original: error: there is no unique constraint matching given keys for referenced table "Profiles"

此错误消息必须是在模型和中间表创建期间出现的。因为它写着profile_houses我在后面的联想中提到过。

这是我的Profile模型:

interface ProfileAttributes {
profileId?: number;
accountId?: number;
ipAddress: string;
pickedHousingIds?: number[];
pickedGymIds?: number[];
createdAt?: Date;
updatedAt?: Date;
deletedAt?: Date;
}
export type ProfileOptionalAttributes = "createdAt" | "updatedAt" | "deletedAt";
export type ProfileCreationAttributes = Optional<ProfileAttributes, ProfileOptionalAttributes>;
export class Profile extends Model<ProfileAttributes, ProfileCreationAttributes> implements ProfileAttributes {
public profileId!: number;
public accountId!: ForeignKey<Account["acctId"]>;
public ipAddress!: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public readonly deletedAt!: Date;
declare getHousings: HasManyGetAssociationsMixin<Housing>;
declare addHousing: HasManyAddAssociationMixin<Housing, number>;
declare addHousings: HasManyAddAssociationsMixin<Housing, number>;
public readonly Housings?: Housing[];
public static associations: {
Housings: Association<Profile, Housing>;
};
static initModel(sequelize: S): typeof Profile {
return Profile.init(
{
profileId: {
type: DataTypes.INTEGER,
unique: true,
autoIncrement: true,
primaryKey: true,
},
ipAddress: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
timestamps: true,
sequelize: sequelize,
paranoid: false,
},
);
}
}

这是我的住房模型:

interface HousingAttributes {
housingId?: number;
address: string;
taskId?: number;
cityId?: number;
stateId?: number;
batchId?: number;
createdAt?: Date;
updatedAt?: Date;
deletedAt?: Date;
}
export type HousingOptionalAttributes = "createdAt" | "updatedAt" | "deletedAt" | "cityId";
export type HousingCreationAttributes = Optional<HousingAttributes, HousingOptionalAttributes>;
export class Housing extends Model<HousingAttributes, HousingCreationAttributes> implements HousingAttributes {
public housingId!: number;
public address!: string;
public taskId!: ForeignKey<Task["taskId"]>;
public cityId!: ForeignKey<City["cityId"]>;
public stateId!: ForeignKey<State["stateId"]>;
public batchId!: ForeignKey<Batch["batchId"]>;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public readonly deletedAt!: Date;
public readonly Profiles?: Profile[];
public static associations: {
Profiles: Association<Housing, Profile>;
};
static initModel(sequelize: Sequelize): typeof Housing {
return Housing.init(
{
housingId: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
address: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
timestamps: true,
sequelize: sequelize,
},
);
}
}

,我将关系初始化为

// https://sequelize.org/docs/v6/advanced-association-concepts/advanced-many-to-many/
Profile.belongsToMany(Housing, { through: "Profile_Housings", as: "chosen_housings" });
Housing.belongsToMany(Profile, { through: "Profile_Housings", as: "housings_chosen_by" });

高级M:N关联的文档表明,到目前为止,我所做的一切都是正确的,除非我误读了它,这是可能的。打字版本看起来也很接近,尽管它们根本没有提到多对多关联。我错过了什么?

编辑:假设现在一切都很好,除了我的db必须被删除和重新制作

作为一个小时前的问题消失了,我唯一的行动,我注意到的区别是删除和重新制作数据库在postgres

相关内容