Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交、删除评论时出错:Error 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails #835

Closed
Big-Cake-jpg opened this issue Apr 14, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@Big-Cake-jpg
Copy link
Contributor

Big-Cake-jpg commented Apr 14, 2024

提交、删除评论时 MySQL 数据库出现问题:

ERROR DELETE FROM `comments` WHERE `comments`.`id` = 406 [5.054401ms] error=Error 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`artalk`.`users`, CONSTRAINT `fk_comments_user` FOREIGN KEY (`id`) REFERENCES `comments` (`user_id`))
ERROR Response: DELETE /api/v2/comments/406 ==> "评论删除失败" host=artalk.example.com id= ip=223.11.xxx.xxx, 39.145.32.31 referer= status=200 user_agent=Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.52 Mobile/15E148 Safari/604.1
ERROR 500: DELETE /api/v2/comments/406   24.408737ms 223.11.xxx.xxx   Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.52 Mobile/15E148 Safari/604.1


ERROR Response: POST /api/v2/comments ==> "评论失败" host=artalk.example.com id= ip=223.11.xxx.xxx, 117.68.3.168 referer= status=200 user_agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
ERROR 500: POST /api/v2/comments   10.825503ms 223.11.xxx.xxx   Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
@qwqcode
Copy link
Member

qwqcode commented Apr 14, 2024

你好,你的 Artalk 版本是 v2.8.4 吗,目前这个版本存在问题,暂时已撤回,可以试试降级到 v2.8.3 😢

@Big-Cake-jpg
Copy link
Contributor Author

你好,你的 Artalk 版本是 v2.8.4 吗,目前这个版本存在问题,暂时已撤回,可以试试降级到 v2.8.3 😢

在发现问题后我也尝试降回 v2.8.3,但是似乎还是存在类似的问题。

话说这个问题具体是因为什么?没研究过 Go 不太能看懂 Artalk 的代码。

@qwqcode
Copy link
Member

qwqcode commented Apr 14, 2024

是因为 v2.8.4 错误地给 comments 表字段加上了外键约束,然而之前的版本没有处理维护外键约束,可以尝试手动删除约束规则。

MySQL 和 PgSQL 数据库会出现这个问题,由于我的疏忽,只测试了 SQLite 它没有问题。

@qwqcode
Copy link
Member

qwqcode commented Apr 14, 2024

可以尝试执行以下的 SQL 语句删除外键约束

--删除 comments 表的 page_key 外键约束
ALTER TABLE comments 
DROP FOREIGN KEY fk_comments_page;

--删除 comments 表的 user_id 外键约束
ALTER TABLE comments 
DROP FOREIGN KEY fk_comments_user;

--删除 pages 表的 site_name 外键约束
ALTER TABLE pages 
DROP FOREIGN KEY fk_pages_site;

@Big-Cake-jpg
Copy link
Contributor Author

可以尝试执行以下的 SQL 语句删除外键约束

--删除 comments 表的 page_key 外键约束
ALTER TABLE comments 
DROP FOREIGN KEY fk_comments_page;

--删除 comments 表的 user_id 外键约束
ALTER TABLE comments 
DROP FOREIGN KEY fk_comments_user;

--删除 pages 表的 site_name 外键约束
ALTER TABLE pages 
DROP FOREIGN KEY fk_pages_site;

在 Navicat 中执行过了以上 SQL 语句,但是在 v2.8.3 上仍然出现了外键约束的问题……?

@qwqcode
Copy link
Member

qwqcode commented Apr 14, 2024

用 Navicat 检查一下 comments 表的约束看看有没有删干净

@Big-Cake-jpg
Copy link
Contributor Author

用 Navicat 检查一下 comments 表的约束看看有没有删干净

好了,现在已经恢复正常了,感谢您不厌其烦的指导 🫡

qwqcode added a commit that referenced this issue Apr 14, 2024
… generation (#835)

Add function to delete all foreign key constraints when migration.

Leave relationship maintenance to the program and reduce the difficulty of database management. Because there are many different DBs and the implementation of foreign keys may be different, and the DB may not support foreign keys, so don't rely on the foreign key function of the DB system.
@qwqcode qwqcode added the bug Something isn't working label Apr 15, 2024
@qwqcode
Copy link
Member

qwqcode commented Apr 25, 2024

你好,v2.8.5 已发布,应该解决了这个问题 :D

@qwqcode qwqcode closed this as completed Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants