knex migrate 抛出错误关系已经存在
knex migrate throws error relation already exists
我使用 Heroku Postgres database and knexjs as SQL query builder in my node.js application. I try to run the latest knex migration,但出现错误 relation already exists
。当我尝试通过 sql 命令 CREATE TABLE
创建 table 时,它可以正常工作。
knexfile.js
// Update with your config settings.
require('dotenv').config({ path: require('find-config')('.env') });
module.exports = {
development: {
client: 'pg',
useNullAsDefault: true,
connection: process.env.DATABASE_URL,
searchPath: ['knex', 'public'],
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
- 最新迁移文件
migrations/20180120183247_users_test.js
/* eslint func-names: 0 */
exports.up = function (knex) {
return knex.schema
.createTable('users', (table) => {
table.increments('id').primary();
table.string('user_name', 100);
table.text('password');
table.string('email', 254);
});
};
exports.down = function (knex) {
return knex.schema
.dropTableIfExists('users');
};
当我运行命令时:
knex migrate:latest --env development
我收到错误:
Using environment: development
Knex:warning - migration file "20180120184707_initial_schema.js" failed
Knex:warning - migration failed with error: create table "users" ("id" serial primary key, "user_name" varchar(100), "password" text, "email" varchar(254)) - relation "users" already exists
error: relation "users" already exists
at Connection.parseE (.../node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (.../node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (.../node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
at TLSSocket.Readable.push (_stream_readable.js:197:10)
at TLSWrap.onread (net.js:589:20)
如果我连接到数据库和 运行 命令:
select * from users;
我得到:
ERROR: relation "users" does not exist
LINE 1: select * from users;
在 Heroku Postgres 设置中 运行 重置数据库以删除所有数据。我等了 2-3 分钟,然后再次尝试 运行 最新的迁移,但我收到了相同的错误消息。
我不知道 knex migrate:latest
命令运行所有迁移文件,所以 table users
是在以前的迁移文件中创建的,这就是问题所在。
我遇到了这个问题,希望有一个简单的解决方案,以防万一其他人想知道您可以做些什么来解决这个问题:
如果您的数据库确实与迁移文件匹配:
进入 knex_migrations
table(或者如果您将其重命名为其他内容,则记录迁移的 table)并添加包含迁移文件名称的行name
列中的问题。
要检查它是否正常工作,请尝试 knex migrate:list
我使用 Heroku Postgres database and knexjs as SQL query builder in my node.js application. I try to run the latest knex migration,但出现错误 relation already exists
。当我尝试通过 sql 命令 CREATE TABLE
创建 table 时,它可以正常工作。
knexfile.js
// Update with your config settings.
require('dotenv').config({ path: require('find-config')('.env') });
module.exports = {
development: {
client: 'pg',
useNullAsDefault: true,
connection: process.env.DATABASE_URL,
searchPath: ['knex', 'public'],
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
- 最新迁移文件
migrations/20180120183247_users_test.js
/* eslint func-names: 0 */
exports.up = function (knex) {
return knex.schema
.createTable('users', (table) => {
table.increments('id').primary();
table.string('user_name', 100);
table.text('password');
table.string('email', 254);
});
};
exports.down = function (knex) {
return knex.schema
.dropTableIfExists('users');
};
当我运行命令时:
knex migrate:latest --env development
我收到错误:
Using environment: development
Knex:warning - migration file "20180120184707_initial_schema.js" failed
Knex:warning - migration failed with error: create table "users" ("id" serial primary key, "user_name" varchar(100), "password" text, "email" varchar(254)) - relation "users" already exists
error: relation "users" already exists
at Connection.parseE (.../node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (.../node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (.../node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
at TLSSocket.Readable.push (_stream_readable.js:197:10)
at TLSWrap.onread (net.js:589:20)
如果我连接到数据库和 运行 命令:
select * from users;
我得到:
ERROR: relation "users" does not exist
LINE 1: select * from users;
在 Heroku Postgres 设置中 运行 重置数据库以删除所有数据。我等了 2-3 分钟,然后再次尝试 运行 最新的迁移,但我收到了相同的错误消息。
我不知道 knex migrate:latest
命令运行所有迁移文件,所以 table users
是在以前的迁移文件中创建的,这就是问题所在。
我遇到了这个问题,希望有一个简单的解决方案,以防万一其他人想知道您可以做些什么来解决这个问题:
如果您的数据库确实与迁移文件匹配:
进入 knex_migrations
table(或者如果您将其重命名为其他内容,则记录迁移的 table)并添加包含迁移文件名称的行name
列中的问题。
要检查它是否正常工作,请尝试 knex migrate:list