I have the following MySQL scripts:
CREATE TABLE user_roles (
id INT AUTO_INCREMENT,
PRIMARY KEY(id),
name TEXT NOT NULL,
access INT NOT NULL DEFAULT '0'
)
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
date_created DATETIME,
roles VARCHAR(50) NOT NULL,
active INT DEFAULT '1',
FOREIGN KEY(roles) REFERENCES user_roles(id)
)
It keeps giving me error 150. Maybe the database isn’t well planned? Any help will be greatly appreciated.
The data types of your
users.rolesanduser_roles.idcolumns must be the same for theFOREIGN KEYconstraint to work correctly. Instead try makingusers.rolesanINT:UPDATE According to comments,
users.rolesshould be text like “admin, moderator, etc.” For correct data normalization,user_roles.idshould be keyed against and to get the text name of the role,JOINthem in queries.