vertically-challenged
@vertically-challenged

Что я делаю не так, почему MySQL ругается?

Мне нужно создать две таблицы, одна дважды ссылается на первую:

create table if not exists users(id int primary key auto_increment, email varchar(255) not null, login varchar(255) not null, password varchar(255) not null)

create table if not exists dialogs (id int primary key auto_increment, user_id_one int not null, user_id_two int not null, foreign key (user_id_one, user_id_two) references users (id, id))


При выполнении второго запроса выводится ошибка:

(1822, "Failed to add the foreign key constraint. Missing index for constraint 'dialogs_ibfk_1' in the referenced table 'users'")
  • Вопрос задан
  • 340 просмотров
Решения вопроса 1
@Akina
Сетевой и системный админ, SQL-программист.
Юзеры в диалогах независимы, потому и связывать их надо независимо.

create table if not exists dialogs (
    id int primary key auto_increment, 
    user_id_one int not null, 
    foreign key (user_id_one) references users (id), 
    user_id_two int not null, 
    foreign key (user_id_two) references users (id)
)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы