为什么我可以使用外国钥匙来获得多个记录?
我正在使用Nodejs和Prisma进行一个后端项目。
在我提出问题之前,一些背景。我有一个指数实体,该实体与一个国家和通过外国钥匙的风险类型相关。因此,在这种情况下,我希望能够将风险类型与多个索引联系起来,而且我也想将一个国家与多个索引联系起来。在这种情况下,我想拥有一个称为X的索引,该索引与风险A型和美国的风险链接,但也与风险A型和国家 /地区有关的指数Z,最后是与风险有关的另一个指数B型和美国国家。
通过运行PRISMA生成机制来自动化数据库定义。
-- CreateTable
CREATE TABLE "risk_type" (
"id" BIGSERIAL NOT NULL,
"name" TEXT NOT NULL,
"parent_id" BIGINT,
"category_id" BIGINT,
"description" TEXT,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "risk_type_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "country" (
"id" BIGSERIAL NOT NULL,
"code" CHAR(3) NOT NULL,
"name" TEXT NOT NULL,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "country_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "index" (
"id" BIGSERIAL NOT NULL,
"name" TEXT,
"risk_type_id" BIGINT NOT NULL,
"country_id" BIGINT NOT NULL,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "index_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "index" ADD CONSTRAINT "index_risk_type_id_fkey" FOREIGN KEY ("risk_type_id") REFERENCES "risk_type"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "index" ADD CONSTRAINT "index_country_id_fkey" FOREIGN KEY ("country_id") REFERENCES "country"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- CreateIndex
CREATE UNIQUE INDEX "index_risk_type_id_key" ON "index"("risk_type_id");
-- CreateIndex
CREATE UNIQUE INDEX "index_country_id_key" ON "index"("country_id");
schema.prisma
定义
model Index {
id BigInt @id @default(autoincrement())
name String?
riskTypeId BigInt @unique @map("risk_type_id")
riskType RiskType @relation(fields: [riskTypeId], references: [id])
countryId BigInt @unique @map("country_id")
country Country @relation(fields: [countryId], references: [id])
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
@@map("index")
}
model Country {
id BigInt @id @default(autoincrement())
code String @unique @db.Char(3)
name String
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
index Index?
@@map("country")
}
model RiskType {
id BigInt @id @default(autoincrement())
name String @unique
description String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
index Index?
@@map("risk_type")
}
问题相当简单&愚蠢,为什么我不能从订单索引记录中插入具有相同值的外国键?例如,如果我对firk_type_id有一个外键'1'索引x,并且我尝试使用firk_type_id的外键'1'创建一个索引z,我会在下面遇到错误。
Prisma查询:
ctx.prisma.index.create({
data: {
name: args.data.name,
country: {
connect: {
id: args.data.country,
},
},
riskType: {
connect: {
id: args.data.riskType,
},
},
},
});
导致此错误:
您要进行的更改将违反
index
和stright> strighttype
模型之间所需的关系“ indextoriskType”。”
。 ,我得到:
SQL查询:
insert into index (name, risk_type_id, country_id)
values ('X', 1, 2)
错误:
[23505]错误:重复的键值违反了唯一约束“ index_risk_type_id_key”详细信息:key(strig_type_id)=(2)已经存在。
当我尝试插入已经分配给索引记录的国家外键时,我的错误i。
我看不到的设计中有缺陷吗?
指数和风险类型之间的关系是错误的吗? (一对一的ATM)
谢谢!
I am working on a backend project using nodeJS and Prisma.
Some context before I ask the question. I have an index entity, that is linked to a country and a risk type through foreign keys. So in this case I would want to be able to link a risk type to more than one index, and also I would like to link a country to more than one index. In this case, I would like to have an index called X that is linked to risk type A and country USA but also an index Z that is linked to risk type A and country DE, and finally, another index Y that is linked to risk type B and country USA.
The database definition was autogenerated by running the Prisma generation mechanism.
-- CreateTable
CREATE TABLE "risk_type" (
"id" BIGSERIAL NOT NULL,
"name" TEXT NOT NULL,
"parent_id" BIGINT,
"category_id" BIGINT,
"description" TEXT,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "risk_type_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "country" (
"id" BIGSERIAL NOT NULL,
"code" CHAR(3) NOT NULL,
"name" TEXT NOT NULL,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "country_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "index" (
"id" BIGSERIAL NOT NULL,
"name" TEXT,
"risk_type_id" BIGINT NOT NULL,
"country_id" BIGINT NOT NULL,
"created_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "index_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "index" ADD CONSTRAINT "index_risk_type_id_fkey" FOREIGN KEY ("risk_type_id") REFERENCES "risk_type"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "index" ADD CONSTRAINT "index_country_id_fkey" FOREIGN KEY ("country_id") REFERENCES "country"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- CreateIndex
CREATE UNIQUE INDEX "index_risk_type_id_key" ON "index"("risk_type_id");
-- CreateIndex
CREATE UNIQUE INDEX "index_country_id_key" ON "index"("country_id");
The schema.prisma
definition
model Index {
id BigInt @id @default(autoincrement())
name String?
riskTypeId BigInt @unique @map("risk_type_id")
riskType RiskType @relation(fields: [riskTypeId], references: [id])
countryId BigInt @unique @map("country_id")
country Country @relation(fields: [countryId], references: [id])
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
@@map("index")
}
model Country {
id BigInt @id @default(autoincrement())
code String @unique @db.Char(3)
name String
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
index Index?
@@map("country")
}
model RiskType {
id BigInt @id @default(autoincrement())
name String @unique
description String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(5)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(5)
index Index?
@@map("risk_type")
}
The question is fairly simple & stupid, why can't I insert foreign keys with the same value from order index records? For example, if I have index X with a foreign key '1' for risk_type_id, and I try to create an index Z with a foreign key '1' for risk_type_id I get the error below.
Prisma query:
ctx.prisma.index.create({
data: {
name: args.data.name,
country: {
connect: {
id: args.data.country,
},
},
riskType: {
connect: {
id: args.data.riskType,
},
},
},
});
results in this error:
The change you are trying to make would violate the required relation 'IndexToRiskType' between the
Index
andRiskType
models."
Also, if I run the SQL command in a SQL query console, I get:
SQL query:
insert into index (name, risk_type_id, country_id)
values ('X', 1, 2)
Error:
[23505] ERROR: duplicate key value violates unique constraint "index_risk_type_id_key" Detail: Key (risk_type_id)=(2) already exists.
The same error I when I try to insert a country foreign key already assigned to an index record.
Is there a flaw in the design that I don't see?
Is the relationship between Index and Risk Type wrong? (one-to-many atm)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论