[英]How to update a NULL foreign key and create a new row within another table referencing the same key?
I have two tables, users
and hubs
.我有两个表,
users
和hubs
。 Once the user creates an account, my backend will make a new row within the users table
(filling in all columns of the row except the hubID
column as it is not known yet).用户创建帐户后,我的后端将在
users table
中创建一个新行(填写该行的所有列,但hubID
列除外,因为它尚不清楚)。
Once the user has signed up, they are greeted with a setup screen, to which where they will obtain a hubID
from an external product that the app connects to.用户注册后,他们会看到一个设置屏幕,他们将从该应用程序连接到的外部产品获取
hubID
。 I want to then update their user row
inside the users table
and add the hubID
to their row (which is currently null) AND at the same time add a new row inside the hubs table
where the primary key
will be the hubID (as it will always be unique).然后我想在
users table
更新他们的user row
,并将hubID
添加到他们的行(当前为空),同时在hubs table
中添加一个新行,其中primary key
将是 hubID(因为它将永远是独一无二的)。
The Users table
has the foreign key
to reference a row inside the hubs table
. Users table
有foreign key
来引用hubs table
的一行。
Users table用户表
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| UserID | int(11) | NO | PRI | NULL | auto_increment |
| Username | varchar(25) | NO | | | |
| Email | varchar(25) | NO | | | |
| Password | varchar(50) | NO | | | |
| hubID | varchar(50) | YES | | | foreign_key |
+----------+-------------+------+-----+---------+----------------+
Hubs Table集线器表
+-----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+----------------+
| hubID | varchar(50)| NO | PRI | | |
| isSetup | BOOLEAN | NO | | 0 | |
| SWVersion | DOUBLE(2,3)| NO | | 0.1 | |
| dateAdded | TIMESTAMP | NO | | | |
+-----------+------------+------+-----+---------+----------------+
At the moment, I am using the query:目前,我正在使用查询:
update users set hubID = 'hub123' where userID = '1';
to add the new hubID to the relevant row inside the users table
.将新的 hubID 添加到
users table
内的相关行。 But I am getting this error:但我收到此错误:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`database`.`users`, CONSTRAINT `fk_users_hubs1` FOREIGN KEY (`hubID`) REFERENCES `hubs` (`hubID`) ON DELETE CASCADE ON UPDATE CASCADE)
I don't know how one would update a foreign key AND add a new row within a different table with the same key so they both represent each other.我不知道如何更新外键并在具有相同键的不同表中添加新行,以便它们相互代表。 My model at the moment would work if I knew the hubID when creating the
user's row
, but that simply is impossible for my design.如果我在创建
user's row
知道 hubID,我的 model 就可以工作,但这对我的设计来说根本不可能。
This is the script:这是脚本:
users table
: users table
:
CREATE TABLE IF NOT EXISTS `database`.`users` (
`userID` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`hubID` VARCHAR(50) NULL,
PRIMARY KEY (`userID`),
INDEX `fk_users_hubs1_idx` (`hubID` ASC) VISIBLE,
CONSTRAINT `fk_users_hubs1`
FOREIGN KEY (`hubID`)
REFERENCES `database`.`hubs` (`hubID`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
hubs table
: hubs table
:
CREATE TABLE IF NOT EXISTS `database`.`hubs` (
`hubID` VARCHAR(50) NOT NULL,
`isSetup` TINYINT NOT NULL DEFAULT 0,
`hubSWVersion` DECIMAL(3,2) NOT NULL DEFAULT 0.1,
`dateAdded` DATETIME NOT NULL,
PRIMARY KEY (`hubID`))
ENGINE = InnoDB;
You probably want user.username
to be unique also.您可能还希望
user.username
是唯一的。 Anyway, I am assuming it is.无论如何,我假设它是。 Even if it isn't there is a remedy I will describe later.
即使没有补救措施,我将在后面描述。 The solution is simply doing inserts in the correct order.
解决方案只是按照正确的顺序进行插入。 For example:
例如:
insert into users(username, email, password) values('Booboo', 'Booboo@nowhere.atall', 'password');
insert into hubs(hubID, isSetup, dateAdded) values('hubBooboo', 1, now());
update users set hubId = 'hubBooboo' where username = 'Booboo';
I am assuming that because username
columns are unique I can do the last update by using the known username
.我假设因为
username
列是唯一的,所以我可以使用已知的username
进行最后更新。 But even if that assumption is not correct, you can always retrieve the last inserted autoinsert key (ie column userId
) with SELECT LAST_INSERT_ID()
and then use column userId
in the where clause.但即使该假设不正确,您始终可以使用
SELECT LAST_INSERT_ID()
检索最后插入的自动插入键(即列userId
),然后在 where 子句中使用列userId
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.