[英]Knex.js postgresql database connection
I'm having a bit of an issue & this is most likely a user error. 我有一个问题,这很可能是用户错误。 I just trying to connect my
Node/Express
application to a database using Knex.js
. 我只是尝试使用
Knex.js
将Node/Express
应用程序连接到数据库。 I'm using Postgres App . 我正在使用Postgres App 。
migrations/20180618143210_item.js
exports.up = function (knex, Promise) {
knex.schema.createTable('items', (table) => {
table.increments('id').primary();
table.string('name');
})
};
exports.down = function (knex, Promise) {
knex.schema.dropTable('items')
};
knexfile.js
module.exports = {
development: {
client: 'postgresql',
connection: {
host : 'localhost',
database: 'my_db',
user: 'User',
password: ''
},
migrations: {
directory: __dirname + '/db/migrations'
},
seeds: {
directory: __dirname + '/db/seeds/development'
}
},
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'
}
}
};
When I run knex migrate:latest
it creates the database and I can see the following \\dt
database tables when connected to my_db
database: 当我运行
knex migrate:latest
它将创建数据库,并且连接到my_db
数据库时,我可以看到以下\\dt
数据库表:
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+-------------
public | knex_migrations | table | User
public | knex_migrations_lock | table | User
When I select * from knex_migrations;
当我
select * from knex_migrations;
I'm returned: 我回来了:
id | name | batch | migration_time
----+------------------------+-------+----------------------------
2 | 20180618143210_item.js | 1 | 2018-06-18 14:40:08.994-06
However, if I try to run something like select * from items
I'm getting a ERROR: relation "items" does not exist
error. 但是,如果我尝试
select * from items
运行类似select * from items
,则会收到ERROR: relation "items" does not exist
错误。 Am I missing something here? 我在这里想念什么吗? The same error is occuring when I try to seed the data
knex seed:run
当我尝试播种数据
knex seed:run
时,会发生相同的错误knex seed:run
items.js
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('items').del()
.then(function () {
// Inserts seed entries
return knex('items').insert([
{id: 1, name: 'rowValue1'},
{id: 2, name: 'rowValue2'},
{id: 3, name: 'rowValue3'}
]);
});
};
Error: error: relation "items" does not exist
. 错误:
error: relation "items" does not exist
。
I figured out why this was occurring. 我弄清楚了为什么会这样。 I wasn't returning anything out of the knex
up
& down
functions. 我没有从knex
up
和down
函数中返回任何东西。 So instead of: 所以代替:
exports.up = function (knex, Promise) {
knex.schema.createTable('items', (table) => {
table.increments('id').primary();
table.string('name');
})
};
exports.down = function (knex, Promise) {
knex.schema.dropTable('items')
};
I needed: 我需要:
exports.up = function (knex, Promise) {
**return** knex.schema.createTable('items', (table) => {
table.increments('id').primary();
table.string('name');
})
};
exports.down = function (knex, Promise) {
**return** knex.schema.dropTable('items')
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.