[英]Error 1452 MySQL and NodeJS. How come the database cant reference my table right?
So im getting Error 1452 from my Node console when inserting a new user account. 因此,在插入新用户帐户时,我从节点控制台收到错误1452。 Here is the error 这是错误
{ Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (
myeherlpertst2
.customers
, CONSTRAINTcustomers_ibfk_1
FOREIGN KEY (user_id
) REFERENCESusers
(user_id
)) {错误:ER_NO_REFERENCED_ROW_2:不能添加或更新子行,外键约束失败(myeherlpertst2
。customers
,约束customers_ibfk_1
外键(user_id
)参考文献users
(user_id
))code: 'ER_NO_REFERENCED_ROW_2', errno: 1452, sqlMessage: 'Cannot add or update a child row: a foreign key constraint fails (
myeherlpertst2
.customers
, CONSTRAINTcustomers_ibfk_1
FOREIGN KEY (user_id
) REFERENCESusers
(user_id
))', sqlState: '23000', index: 0, 代码: 'ER_NO_REFERENCED_ROW_2',错误号:1452,sqlMessage: '不能添加或更新子行,外键约束失败(myeherlpertst2
。customers
,约束customers_ibfk_1
外键(user_id
)参考文献users
(user_id
))',SQLSTATE:“23000 ',索引:0,
Im very sure that the reason I am getting this error is because the customers table is trying to reference something that isnt there BUT I dont understand why its not referencing it. 我非常确定我收到此错误的原因是因为客户表试图引用那里没有的东西,但我不明白为什么它没有引用它。 In my Node code below I insert the user table first which auto increments user_id and is not null. 在下面的节点代码中,我首先插入用户表,该表将自动递增user_id,并且不为null。 Do I have to end the transaction for the first insert and then start a new one before I can insert into the customers table? 在插入客户表之前,我是否必须结束第一次插入的交易,然后开始新的交易?
connection.beginTransaction(function(error){
if(error){
console.log('Error Caught');
return res;
}
if (error) {throw error;
console.log('Error Caught');
return;}
connection.query('Insert into users( user_name, user_password, user_type) values (?,?,?)', [customer.body.userName, customer.body.password, userType=1], function (error, results, fields) {
if (error) {
console.log(error);
return;
}
connection.query('Insert into customers(cust_first_name, cust_last_name, cust_email, cust_city, cust_address, cust_zip_code, cust_state, cust_phone_num, cust_role, cust_website, cust_business_name) values (?,?,?,?,?,?,?,?,?,?,?)', [customer.body.firstName, customer.body.lastName, customer.body.email, customer.body.city, customer.body.address, customer.body.zipCode, customer.body.state, customer.body.phoneNumber, customer.body.custRole=1, customer.body.website, customer.body.businessName], function (error, results, fields) {
if (error) {
console.log(error);
}
});
res.end(JSON.stringify(results));
});
console.log('End Transaction')
});
You need to set the user_id
column correctly in the customers
table. 您需要在customers
表中正确设置user_id
列。 MySQL's LAST_INSERT_ID() function is the source of the value you need. MySQL的LAST_INSERT_ID()函数是您需要的值的来源。 Here's how to do that, in SQL. 这是使用SQL的方法。
Insert into users( user_name, user_password, user_type) values (?,?,?);
Insert into customers(user_id, cust_first_name, cust_last_name, cust_email,
cust_city, cust_address, cust_zip_code, cust_state,
cust_phone_num, cust_role, cust_website, cust_business_name)
values (LAST_INSERT_ID(),?,?,?,?,?,?,?,?,?,?,?);
Notice that the insert into the customers table changes the value of LAST_INSERT_ID(), to the value of the autoincrementing ID in that table. 注意,插入到customers表中的值将LAST_INSERT_ID()的值更改为该表中自动递增ID的值。 So, if you need to reuse the value from the users table, do this, with three queries rather than two: 因此,如果您需要重用users表中的值,请使用三个查询而不是两个查询来执行此操作:
Insert into users( user_name, user_password, user_type) values (?,?,?);
SET @userid := LAST_INSERT_ID();
Insert into customers(user_id, cust_first_name, cust_last_name, cust_email,
cust_city, cust_address, cust_zip_code, cust_state,
cust_phone_num, cust_role, cust_website, cust_business_name)
values (@userid,?,?,?,?,?,?,?,?,?,?,?);
I'll leave it to you to put the queries into your node program. 我将它留给您,以将查询放入您的节点程序中。
No need for another query : The result of an insert contain the last autoincrement insert id 无需其他查询:插入的结果包含最后一个自动增量插入ID
This information is available with results.insertId. 该信息可通过results.insertId获得。
So corrrection would be like : 所以改正就像:
connection.query(
"Insert into users( user_name, user_password, user_type) values (?,?,?)",
[customer.body.userName, customer.body.password, (userType = 1)],
function(error, results, fields) {
if (error) {
console.log(error);
return;
}
connection.query(
"Insert into customers(user_id, cust_first_name, cust_last_name, cust_email, cust_city, cust_address, cust_zip_code, cust_state, cust_phone_num, cust_role, cust_website, cust_business_name) values (?,?,?,?,?,?,?,?,?,?,?,?)",
[
results.insertId,
customer.body.firstName,
customer.body.lastName,
customer.body.email,
customer.body.city,
customer.body.address,
customer.body.zipCode,
customer.body.state,
customer.body.phoneNumber,
(customer.body.custRole = 1),
customer.body.website,
customer.body.businessName
],
function(error, results, fields) {
if (error) {
console.log(error);
}
}
);
res.end(JSON.stringify(results));
}
);
btw: the parameter userType=1 must probably be an error 顺便说一句:参数userType = 1可能必须是一个错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.