postgresql - Error when inserting data into a database
问题描述
For school we needed to create a database. I wrote the code to create the database but i have problems with inserting data into it. I always get the same error.
ERROR: insert or update on table "gebruikers" violates foreign key constraint "fk_gebruikers_afspeellijsten" DETAIL: Key (userid)=(1) is not present in table "afspeellijsten". SQL state: 23503
I created the "gebruikers" and the "afspeellijsten" tables likes this:
CREATE SCHEMA GROEP10;
CREATE TABLE GROEP10.Gebruikers
(
userID INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL,
wachtwoord VARCHAR(45) NOT NULL,
mail VARCHAR(45) UNIQUE NOT NULL,
birthDate DATE NULL,
CONSTRAINT fk_Gebruikers_Afspeellijsten FOREIGN KEY ( userID ) REFERENCES GROEP10.Afspeellijsten (userID)
);
CREATE TABLE GROEP10.Afspeellijsten
(
afspeellijstID INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL,
creationDate DATE NOT NULL,
userID INTEGER NOT NULL,
CONSTRAINT fk_Afspeellijsten_Gebruikers UNIQUE ( userID ),
CONSTRAINT fk_Afspeellijsten_AfspeellijstHasSingle FOREIGN KEY ( afspeellijstID ) REFERENCES GROEP10.AfspeellijstHasSingle ( afspeellijstID )
);
I try to insert data into gebruikers like this:
INSERT INTO gebruikers
VALUES (
1,
'Jonas',
'OK',
'ZZZ@hotmail.com',
'03/03/2000'
);
解决方案
在您的表中,Gebruikers
您有一个FOREIGN KEY CONSTRAINTuserID
CONSTRAINT fk_Gebruikers_Afspeellijsten FOREIGN KEY ( userID )
REFERENCES GROEP10.Afspeellijsten (userID)
这意味着在您可以插入之前,Gebruikers
您必须userID = 1
在Afspeellijsten
推荐阅读
- kubernetes - GKE K8 HPA 无法获取堆栈驱动程序指标
- android - Android中的图像缓存
- spring - 带有路径变量和请求参数的 Swagger codegen maven 插件 OpenApi 产生不工作的代码
- android - 图片正在上传旋转到我的 Android 项目中的服务器?
- angular - 数据未加载到Angular 8中的输入字段
- c# - Console.ReadLine() 在 VS Code 中不起作用,用 C# 编写代码
- android - 我想在字符串中搜索一个特定的单词,并在 android studio 中找到该单词之前显示 4 个单词和 4 个单词之后显示
- node.js - 我弄乱了用于安装和运行节点 (npm) 的全局目录
- spring-boot - 为 Spring Retry max atttemps 编写 Junit 测试用例
- python - 应用于返回意外结果的数据框列