如何在 MySQL 中真正创建外键?

发布于 2025-01-15 19:29:28 字数 665 浏览 2 评论 0原文

这听起来像是一个愚蠢的问题,因为实际上有数百个教程。

但无论我按照哪个教程创建外键,或者在我的例子中,创建复合外键,即使表创建总是成功,MySQL Workbench 也不会显示任何外键信息。

如果我创建这两个示例表

CREATE TABLE parent (
id INT NOT NULL,
category VARCHAR(255) NOT NULL,
PRIMARY KEY (id, category)
);

CREATE TABLE child (
id INT PRIMARY KEY,
category VARCHAR(255) NOT NULL,
info TEXT,
CONSTRAINT fk FOREIGN KEY (id, category) REFERENCES parent(id, category)
);

表创建成功,但未列出外键。 输入图片此处描述

我缺少什么?这确实是很奇怪的行为,它们既不是 MySQL Workbench 显示的错误也不是警告。

This sounds like a stupid question, because there's literally hundreds of tutorials out there.

But no matter which tutorial I follow to create a foreign key or, in my case, a composite foreign key, even though the table creation is always successful, MySQL Workbench does not show any foreign key information.

If I create these 2 example tables

CREATE TABLE parent (
id INT NOT NULL,
category VARCHAR(255) NOT NULL,
PRIMARY KEY (id, category)
);

CREATE TABLE child (
id INT PRIMARY KEY,
category VARCHAR(255) NOT NULL,
info TEXT,
CONSTRAINT fk FOREIGN KEY (id, category) REFERENCES parent(id, category)
);

The table creation is successful but the foreign key is not listed.
enter image description here

What am I missing? This is really weird behavior that their is neither an error nor a warning shown by MySQL Workbench.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2025-01-22 19:29:28

我在 MySQL Workbench 8.0.28 中测试了您的示例表。它使用其外键成功创建了表。我运行了 SHOW CREATE TABLE child ,输出显示了外键。

但是 MySQL Workbench 显然有一个在可视表信息中显示外键的错误。我像你一样查看了外键选项卡。我按下“刷新”按钮并收到以下消息:

错误

未处理的异常:结果集的列约束名称无效

检查日志以获取更多详细信息。

我检查了日志(帮助->显示日志文件)并看到了以下内容:

10:15:53 [ERR][sqlide_tableman_ext.py:show_table:1186]: Error initializing tab constraints: Traceback (most recent call last):
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_tableman_ext.py", line 1183, in show_table
    tab.show_table(schema, table)
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_tableman_ext.py", line 854, in show_table
    self.refresh()
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_catalogman_ext.py", line 373, in refresh
    self.preload_data(self.get_query())
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_catalogman_ext.py", line 367, in preload_data
    node.set_string(i, rset.stringFieldValueByName(field) or "" if format_func is None else format_func(rset.stringFieldValueByName(field)))
SystemError: invalid column constraint_name for resultset

此错误于 2021 年 2 月报告:https://bugs.mysql.com/bug.php?id=102496(以及上面评论中的重复错误 ysth 链接)。该错误是“已验证”,这意味着 MySQL 团队承认它是一个错误,但没有修复,也没有估计何时修复。

I tested your example tables in MySQL Workbench 8.0.28. It successfully created the child table with its foreign key. I ran SHOW CREATE TABLE child and the output shows the foreign key.

But MySQL Workbench apparently has a bug showing the foreign key in the visual table information. I viewed the foreign keys tab as you did. I pressed the "refresh" button and got this message:

Error

Unhandled exception: invalid column constraint_name for resultset

Check the log for more details.

I checked the log (Help->Show Log File) and saw this:

10:15:53 [ERR][sqlide_tableman_ext.py:show_table:1186]: Error initializing tab constraints: Traceback (most recent call last):
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_tableman_ext.py", line 1183, in show_table
    tab.show_table(schema, table)
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_tableman_ext.py", line 854, in show_table
    self.refresh()
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_catalogman_ext.py", line 373, in refresh
    self.preload_data(self.get_query())
  File "/Applications/MySQLWorkbench.app/Contents/Resources/plugins/sqlide_catalogman_ext.py", line 367, in preload_data
    node.set_string(i, rset.stringFieldValueByName(field) or "" if format_func is None else format_func(rset.stringFieldValueByName(field)))
SystemError: invalid column constraint_name for resultset

This bug was reported in February 2021: https://bugs.mysql.com/bug.php?id=102496 (and also the duplicate bug ysth links to in the comments above). The bug is "Verified" which means the MySQL team acknowledges it as a bug, but there is no fix and no estimate for when there will be a fix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文