[英]Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
I'm using Laravel 5.5. 我正在使用Laravel 5.5。 I've my database ready and i have 2 tables.
我已经准备好数据库,并且有2个表。 1)
users
2) users_auth_tokens
There's no any data filled in users
table right now. 1)
users
2) users_auth_tokens
目前, users
表中没有任何数据。
So, my requirement is for first time any third user call api to get refreshToken
for android device and i will insert data( 'auth_token'
, 'refresh_token'
, 'token_validity'
) into users_auth_tokens
table when it called. 所以,我的要求是,任何第三次用户调用api
refreshToken
为Android设备获取refreshToken
,并且在调用时,我会将数据( 'auth_token'
, 'refresh_token'
, 'token_validity'
)插入到users_auth_tokens
表中。 There's one field user_id
which i want to insert '0'
for first time api call and when any user make call api after login, i want to update same record(which is user_id=0
) with the logged-in user_id
. 我要在其中一个字段
user_id
进行首次api调用插入'0'
,并且当任何用户登录后进行调用api时,我都想使用登录的user_id
更新相同的记录( user_id=0
)。
But unfortunately, when i was trying to insert record with user_id=0
it's give me error like: 但是不幸的是,当我尝试插入
user_id=0
记录时,它给了我这样的错误:
"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
tracker
.users_auth_tokens
, CONSTRAINTusers_auth_tokens_user_id_foreign
FOREIGN KEY (user_id
) REFERENCESusers
(id
) ON DELETE CASCADE) (SQL: insert intousers_auth_tokens
(auth_token
,validity
,refresh_token
,updated_at
,created_at
) values (ETZEI11RY3D7RQE4XR6OBTNQHC6Q1ND187E5FD3E45281D3385D784C3D89C0697D46DDC, 1524135471, b87c88fdcf1eea0a6680620d8da77e4f0ee18b78, 2018-04-19 10:52:51, 2018-04-19 10:52:51))"“ SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败(
tracker
users_auth_tokens
,CONSTRAINTusers_auth_tokens_user_id_foreign
FOREIGN KEY(user_id
)参考users
(id
)删除users_auth_tokens_user_id_foreign
(SQL:插入到users_auth_tokens
(auth_token
,validity
,refresh_token
,updated_at
,created_at
)值(ETZEI11RY3D7RQE4XR6OBTNQHC6Q1ND187E5FD3E45281D3385D784C3D89C0697D46DDC,1524135471,b87c88fdcf1eea0a6680620d8da77e4f0ee18b78,2018年4月19日10时52分51秒,2018年4月19日10时52分51秒))”
I know that why this error occurs because there's foreign key user_id
in users_auth_tokens
and there's no any record in users
table right now! 我知道为什么会发生此错误,因为在
users_auth_tokens
有外键user_id
,而在users
表中现在没有任何记录! I've tried with default 0 value from migration and make it nullable also, but no luck. 我尝试从迁移中使用默认0值并使它也可以为空,但是没有运气。
Anyway i want to insert user_id=0
in that table. 无论如何,我想在该表中插入
user_id=0
。 I search some tricks on google but no one helps. 我在Google上搜索了一些技巧,但没有人帮助。
My code is like: 我的代码是这样的:
$usersAuthTokens = new UsersAuthTokens;
$usersAuthTokens->auth_token = $authToken;
$usersAuthTokens->validity = $time;
$usersAuthTokens->refresh_token = $newRefreshToken;
$usersAuthTokens->user_id = 0; //error is here..because no record in users table right now
$usersAuthTokens->save();
Anyone have an solution for this? 有人对此有解决方案吗? Any helps will be appreciated!!
任何帮助将不胜感激!
Thanks. 谢谢。
I've fixed it with saving null
user_id but not with 0
. 我通过保存
null
user_id而不是0
。
I need to make ->nullable()
in migration file and when I am saving the record, I also need to save null
value! 我需要在迁移文件中设置
->nullable()
,并且在保存记录时,还需要保存null
值!
I tried to save user_id=0
but no luck and I've fixed it with null
value! 我试图保存
user_id=0
但没有运气,我用null
值修复了它!
Thanks for your help @JonasStaudenmeir! 感谢您的帮助@JonasStaudenmeir!
Hope this will helps any other user in the future if they face the same issue! 希望这对将来遇到其他问题的其他用户有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.