1

私は次のMySQLスクリプトを持っています:

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)
)

エラー150が発生し続けます。データベースが適切に計画されていない可能性がありますか?どんな助けでも大歓迎です。

4

2 に答える 2

2

The data types of your users.roles and user_roles.id columns must be the same for the FOREIGN KEY constraint to work correctly. Instead try making users.roles an INT:

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,
    -- Change this...
    roles INT NOT NULL,
    active INT DEFAULT '1',
    FOREIGN KEY(roles) REFERENCES user_roles(id)
)

UPDATE According to comments, users.roles should be text like "admin, moderator, etc." For correct data normalization, user_roles.id should be keyed against and to get the text name of the role, JOIN them in queries.

于 2011-09-14T21:04:46.733 に答える
1

ステートメントをセミコロンで区切り、文字列の代わりに INTS を使用する必要があります。

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)
);
于 2011-09-14T21:08:41.603 に答える