MySQL #1072 - 表中不存在键列“role_id”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18916650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:54:55  来源:igfitidea点击:

#1072 - Key column 'role_id' doesn't exist in table

mysqlsql

提问by JeraldPunx

I am trying to add foreign key on my other table but this gave me error #1072 - Key column 'role_id' doesn't exist in table

我试图在我的另一个表上添加外键,但这给了我错误 #1072 - Key column 'role_id' doesn't exist in table

I have created a table named role

我创建了一个名为的表 role

then I created like this

然后我像这样创建

create table role (
  role_id varchar(15)
  primary key (role_id)
)

then when I try to alter table on my usertable

然后当我尝试更改桌子上的user桌子时

alter table user
add foreign key (role_id)
references role(role_id)

and I got an error like this

我得到了这样的错误

#1072 - Key column 'role_id' doesn't exist in table

#1072 - Key column 'role_id' doesn't exist in table

采纳答案by Filipe Silva

You have to have the column you reference in add foreign key (role_id)inside your user table. Otherwise you get that error.

您必须在用户表中的add foreign key (role_id 中)包含您引用的列。否则你会得到那个错误。

You would have to have inside your user table something like:

您必须在用户表中包含以下内容:

create table user(
  ...
  role_id varchar(15)
  ...
)

Or if you don't have it, you have to do:

或者,如果您没有它,则必须执行以下操作:

ALTER TABLE user ADD COLUMN role_id VARCHAR(15)

Before you set it as a foreign key.

在将其设置为外键之前。

回答by Besnik

The Table userdoes not have the column role_id

user没有列role_id