MySQL - 无法添加外键约束 - Latin/Cyrillic

MySQL - Cannot add foreign key constraint - Latin/Cyrillic

这是我最后的选择,因为我已经尝试了在 Google 和 Stack Overflow 上找到的所有内容。

在问这个问题之前,我已经彻底搜索了答案。我发现人们从 MySQL 收到了相同的错误消息,但他们的解决方案 对我有用,因为我没有做那个特定的事情(不同的列类型) 错误的。

我有一个外键问题,它们 相同的类型(varchar(xx),其中 xx 在两种情况下的长度相同;不是 NULL),但是仍然给我一个错误,PHPMyAdmin 的命令 SHOW ENGINE INNODB STATUS 将其描述为:

2015-03-24 16:04:21 66c Error in foreign key constraint of table arhiva/privilegija:
FOREIGN KEY (korisnik_ime) REFERENCES korisnik(ime) ,
FOREIGN KEY (firma_naziv) REFERENCES firma(naziv)
):
Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint.

这是我的数据库架构,对于非英文名称很抱歉(顺便说一句,我第一次遇到这个问题时,列名称是西里尔字符,但即使在我将它们转换为拉丁文之后 - 你所看到的在这里 - 我的问题 没有 解决):

DROP DATABASE IF EXISTS arhiva;
CREATE DATABASE arhiva
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_general_ci;
USE arhiva;

CREATE TABLE `korisnik` (
  ime VARCHAR(30) NOT NULL ,
  lozinka CHAR(60) NOT NULL
);

CREATE TABLE `firma` (
  naziv VARCHAR(50) NOT NULL
);

CREATE TABLE `privilegija` (
  korisnik_ime VARCHAR(30) NOT NULL ,
  firma_naziv VARCHAR(50) NOT NULL ,
  pristap CHAR(1) NOT NULL ,
  FOREIGN KEY (korisnik_ime) REFERENCES korisnik(ime) ,
  FOREIGN KEY (firma_naziv) REFERENCES firma(naziv)
);

你能指导我解决这个问题的正确方向吗?我认为使用西里尔字符是问题所在,但事实证明问题出在其他地方。

添加PRIMARY KEY(),像这样:

CREATE TABLE `korisnik` (
ime VARCHAR(30) NOT NULL ,
lozinka CHAR(60) NOT NULL,
PRIMARY KEY(ime)
);

CREATE TABLE `firma` (
naziv VARCHAR(50) NOT NULL,
PRIMARY KEY(naziv)
);

现在你有了一个索引,所以外键约束应该可以工作了。