table 约束中不存在 Postgres key(keyId, keyId, keyId)=(2, 2, 2)
Postgres key(keyId, keyId, keyId)=(2, 2, 2) is not present in table constraint
当我开始为我的数据库播种时,一切都很好,但是当我尝试将第二组报价添加到同一用户的第二个列表时(第一组报价成功添加到用户的第一个列表)
/entity/User.ts
@Entity('users') // @note for postgres naming conflict
export class User extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type: 'text', unique: true })
username: string
@OneToMany(() => Listing, listing => listing.user)
listings: Listing[]
}
/entity/Listing.ts 用户 => 1:M => 列表
@Entity()
export class Listing extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type: 'varchar', length: 255 })
title: string
@OneToMany(() => Offer, offer => offer.listing)
offers: Offer[]
@PrimaryColumn()
userId: number
@ManyToOne(() => User, user => user.listings)
@JoinColumn({ name: 'userId' })
user: Promise<User>
}
/entity/Offer.ts 列表 => 1:M => 报价
@Entity()
export class Offer extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column('int') price: number
@PrimaryColumn()
listingId: number
@ManyToOne(() => Listing, listing => listing.offers)
@JoinColumn({ name: 'listingId' })
listing: Promise<Listing>
}
下面是播种数据库的代码:
const users = [
{
id: 1,
username: faker.internet.userName(),
}
]
const listings = [
{
id: 1,
title: faker.lorem.sentence(),
userId: 1
},
{
id: 2,
title: faker.lorem.sentence(),
userId: 1,
}
]
const offers = [
{
id: 1,
price: faker.random.number(priceOptions),
listingId: 1,
},
{
id: 2,
price: faker.random.number(priceOptions),
listingId: 2,
}
]
export const startSeeding = async () => {
await users.map(user => User.create(user).save())
await listings.map(listing => Listing.create(listing).save())
setTimeout(async () => {
await offers.map(offer => Offer.create(offer).save())
}, 3000) // for sure
}
错误信息:
query failed: INSERT INTO "offer"("price", "wage", "hoursOfWeek", "housing", "tips", "overtimes", "notes", "offerTypeId", "positionId", "listingId") VALUES (, , , , , , , , , ) RETURNING "id", "housing", "tips", "overtimes" -- PARAMETERS: [256,16,32,true,true,true,"Qui dolores assumenda doloremque doloribus.",1,2,2]
error: { error: insert or update on table "offer" violates foreign key constraint "FK_d33dccabb2f699c92920ff197c8"
detail: 'Key (listingId, listingId, listingId)=(2, 2, 2) is not present in table "listing".',
await users.map(user => User.create(user).save())
错了
你应该做的
await Promise.all(users.map(user => User.create(user).save()))
我发现我的代码有什么问题,我使用 PrimaryColumn
来表示 1:M 关系,但我只需要一个 Column
当我开始为我的数据库播种时,一切都很好,但是当我尝试将第二组报价添加到同一用户的第二个列表时(第一组报价成功添加到用户的第一个列表)
/entity/User.ts
@Entity('users') // @note for postgres naming conflict
export class User extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type: 'text', unique: true })
username: string
@OneToMany(() => Listing, listing => listing.user)
listings: Listing[]
}
/entity/Listing.ts 用户 => 1:M => 列表
@Entity()
export class Listing extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type: 'varchar', length: 255 })
title: string
@OneToMany(() => Offer, offer => offer.listing)
offers: Offer[]
@PrimaryColumn()
userId: number
@ManyToOne(() => User, user => user.listings)
@JoinColumn({ name: 'userId' })
user: Promise<User>
}
/entity/Offer.ts 列表 => 1:M => 报价
@Entity()
export class Offer extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column('int') price: number
@PrimaryColumn()
listingId: number
@ManyToOne(() => Listing, listing => listing.offers)
@JoinColumn({ name: 'listingId' })
listing: Promise<Listing>
}
下面是播种数据库的代码:
const users = [
{
id: 1,
username: faker.internet.userName(),
}
]
const listings = [
{
id: 1,
title: faker.lorem.sentence(),
userId: 1
},
{
id: 2,
title: faker.lorem.sentence(),
userId: 1,
}
]
const offers = [
{
id: 1,
price: faker.random.number(priceOptions),
listingId: 1,
},
{
id: 2,
price: faker.random.number(priceOptions),
listingId: 2,
}
]
export const startSeeding = async () => {
await users.map(user => User.create(user).save())
await listings.map(listing => Listing.create(listing).save())
setTimeout(async () => {
await offers.map(offer => Offer.create(offer).save())
}, 3000) // for sure
}
错误信息:
query failed: INSERT INTO "offer"("price", "wage", "hoursOfWeek", "housing", "tips", "overtimes", "notes", "offerTypeId", "positionId", "listingId") VALUES (, , , , , , , , , ) RETURNING "id", "housing", "tips", "overtimes" -- PARAMETERS: [256,16,32,true,true,true,"Qui dolores assumenda doloremque doloribus.",1,2,2]
error: { error: insert or update on table "offer" violates foreign key constraint "FK_d33dccabb2f699c92920ff197c8"
detail: 'Key (listingId, listingId, listingId)=(2, 2, 2) is not present in table "listing".',
await users.map(user => User.create(user).save())
错了
你应该做的
await Promise.all(users.map(user => User.create(user).save()))
我发现我的代码有什么问题,我使用 PrimaryColumn
来表示 1:M 关系,但我只需要一个 Column