mariadb won't create foreign key

泪湿孤枕 提交于 2021-01-29 10:54:13

问题


I think I'm using the right syntax for MariaDB, but my foreign key constraint is not being created.

Here's the create table DDL:

CREATE TABLE items (
  id INT auto_increment primary key,
  description TEXT NOT NULL
);

CREATE TABLE item_events (
  id INT NOT NULL,
  calendar_event_guid TEXT(255) NOT NULL,
  foreign key item_events_id_fk (id) REFERENCES items (id)
);

Then, when I ask MariaDB to show me what I created, I get this:

+-------------+-----------------
| Table       | Create Table                                                                                                                                                                                                       +-------------+-----------------
| item_events | CREATE TABLE `item_events` (
  `id` int(11) NOT NULL,
  `calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
  KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------------+-----------------

or, just showing the DDL:

CREATE TABLE `item_events` (
  `id` int(11) NOT NULL,
  `calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
  KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Notice that it only created a "KEY", not a foreign key. The items table is correctly created.

Surely, this is really simple :)

来源:https://stackoverflow.com/questions/65361910/mariadb-wont-create-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!