将一个表的uuid传递到另一个表作为postgresql中的外键值
我有table 员工
在Postgres中:
drop table if exists employee;
create table employee (
id uuid default uuid_generate_v4 () primary key,
first_name varchar not null,
last_name varchar not null
);
另一个表salary
:
drop table if exists salary;
create table salary (
check_id uuid default uuid_generate_v4 () primary key,
salary int not null,
employee_id uuid references employee (id)
);
lightee_id
是ID
中的外键表,但我不明白如何在雇员
中插入一个值,因为UUID是唯一的。
我将值插入雇员
表:
insert into employee (first_name, last_name, email, code) values ( 'jonh', 'smith', '[email protected]', '1');
然后,如果我尝试将值插入salary
表:
insert into salary (salary ) values ('1000');
然后 select 命令将返回雇员>雇员> /代码>值空。
但是,如果我将其制作默认uuid_generate_v4()
,则结果为:key(losperee_id)=(C4CCD745-02BA-4A0E-8586-32E3C6A2B84A)不存在于表“雇员”中。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<。 /code>
我明白,因为lightee_id
是一个外键,它应该与雇员
中的uuid匹配,但是由于uuid大多是唯一的,我该如何使其正常工作?
I have table Employee
in Postgres:
drop table if exists employee;
create table employee (
id uuid default uuid_generate_v4 () primary key,
first_name varchar not null,
last_name varchar not null
);
And another table salary
:
drop table if exists salary;
create table salary (
check_id uuid default uuid_generate_v4 () primary key,
salary int not null,
employee_id uuid references employee (id)
);
employee_id
is the foreign key to id
in the Employee table, but I don't understand how to insert a value inside employee_id
since UUID is unique.
I am inserting values into Employee
table:
insert into employee (first_name, last_name, email, code) values ( 'jonh', 'smith', '[email protected]', '1');
And then if I try insert values into salary
table:
insert into salary (salary ) values ('1000');
Then select
command will return employee_id
value empty.
But if I make it default uuid_generate_v4 ()
, then result is: Key (employee_id)=(c4ccd745-02ba-4a0e-8586-32e3c6a2b84a) is not present in table "employee".
I understand that because employee_id
is a foreign key it should match with uuid in employee
, but since uuid is mostly unique, how can I make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用已插入
雇员
表的uuid
。您可以使用a cte 一个语句:You have to use the
uuid
that was inserted into theemployee
table. You can do this with a CTE in a single statement: