MySQL: ОШИБКА 1822 (HY000) Не удалось добавить ограничение внешнего ключа. Отсутствует индекс для ограничения - PullRequest
0 голосов
/ 07 марта 2020

Я пытаюсь создать таблицу с внешними ключами. Когда я выполняю инструкцию CREATE TABLE, как показано ниже, я получаю это сообщение об ошибке.

ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'fk_opentran_helper' in the referenced table 'helper'

CREATE TABLE Statement

CREATE TABLE helper_work_day(
work_day_id TINYINT,
helper_id SMALLINT(4) NOT NULL,
office_code DECIMAL(10) NOT NULL,
work_day TINYINT NOT NULL,
kana VARCHAR(32) NOT NULL,
can_holiday_work BIT(1) NOT NULL,
CONSTRAINT pk_open_transaction PRIMARY KEY (office_code, helper_id, work_day_id),
CONSTRAINT fk_opentran_helper FOREIGN KEY (helper_id) references helper (id),
CONSTRAINT fk_opentran_office FOREIGN KEY (office_code) references office (office_code)
);

Информация о 'helper' TABLE

mysql> SHOW COLUMNS FROM helper;
+------------------+---------------+------+-----+---------+-------+
| Field            | Type          | Null | Key | Default | Extra |
+------------------+---------------+------+-----+---------+-------+
| id               | smallint(4)   | NO   | PRI | NULL    |       |
| office_code      | decimal(10,0) | NO   | PRI | NULL    |       |
| name             | varchar(32)   | NO   |     | NULL    |       |
| kana             | varchar(32)   | NO   |     | NULL    |       |
| can_holiday_work | bit(1)        | NO   |     | NULL    |       |
| login_id         | varchar(16)   | NO   |     | NULL    |       |
| password         | char(60)      | NO   |     | NULL    |       |
+------------------+---------------+------+-----+---------+-------+
7 rows in set (0.04 sec)

mysql> SHOW INDEX FROM helper;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| helper |          0 | PRIMARY  |            1 | office_code | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| helper |          0 | PRIMARY  |            2 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.06 sec)

Что я пробовал

  • Убедитесь, что столбец 'id' в таблице 'helper' имеет индекс
  • Убедитесь, что 'id' столбец (таблица 'helper') и столбец 'helper_id' (таблица 'helper_work_day') имеют одинаковый тип данных

1 Ответ

0 голосов
/ 08 марта 2020

Благодаря комментарию Славы Рожнева мне удалось создать эту таблицу. Я добавил индекс для столбца id (в таблице'helper ') с этим оператором.

ALTER TABLE helper ADD INDEX index_id(id);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...