2017-08-21 10 views
2

現在、新しいテーブルをデータベースに移行しようとしています。このテーブルはユーザテーブルです。これは、2つのエラーを返しユニークな制約関数を持つKnex.jsの移行

exports.up = function(knex, Promise) { 
    return knex.schema.createTableIfNotExists('users', function(table) { 
    table.increments('id').primary(); 
    table.string('first_name').notNullable(); 
    table.string('last_name').notNullable(); 
    table.string('email').unique().notNullable(); 
    table.string('password').notNullable(); 
    table.timestamps(false, true); 
    }).then(() => { 
    console.log('Users Table is Created!'); 
    }) 
}; 

exports.down = function(knex, Promies) { 
    return knex.schema.dropTable('users') 
    .then(() => { 
     console.log('Users Table has been Dropped!'); 
    }) 
}; 

:ここで私が持っているコードがある

Knex:warning - migrations failed with error: alter tableusersadd uniqueusers_email_unique(email) - Key column 'email' doesn't exist in table

Error: Key column 'email' doesn't exist in table

unique()がやろうとしていることにALTER TABLEをプリフォームであることが表示されます私が作成しようとしているテーブル。したがって、電子メール列が存在しないというエラーです。ここで何か間違っているのですか、テーブルを作成するときに列のunique()関数を実行できませんか?私は、ドキュメント上のcreateTableでunique()関数を使用する例を見ました。だから私はなぜこれが上記のエラーで返答するのかということを犠牲にしている。

私のデータベースが作成されており、私はそれを見ることができます。 unique()という制約は、作成されない電子メール列には適用されません。

答えて

0

したがって、すべてのテーブルを削除してマイグレーションを再実行する必要があることが判明しました。それがなぜ機能していないのか、それがalter tableを実行しようとしていたと思われる理由です。

0
knex.schema.createTableIfNotExists('users', function(table) { 
    table.increments('id').primary(); 
    table.string('first_name').notNullable(); 
    table.string('last_name').notNullable(); 
    table.string('email').unique().notNullable(); 
    table.string('password').notNullable(); 
    table.timestamps(false, true); 
    }).toSQL().forEach(query => console.log(query.sql)) 

テーブルがすでにその名前の制約、それは失敗しますクエリを作成制約がある場合は、次のSQLそう

create table if not exists "users" ("id" serial primary key, "first_name" varchar(255) not null, "last_name" varchar(255) not null, "email" varchar(255) not null, "password" varchar(255) not null, "created_at" timestamptz not null default CURRENT_TIMESTAMP, "updated_at" timestamptz not null default CURRENT_TIMESTAMP); 
alter table "users" add constraint "users_email_unique" unique ("email"); 

を作成します。しかし、あなたのエラーは、他の奇妙な状況を参照しているようです。

関連する問題