在使用 Knex.js 时,我做了一个迁移和种子,但是种子数据没有出现在 PostgreSQL 数据库中
While using Knex.js I made a migration and seed, but the seed data dose not show up in the PostgreSQL database
所以我不得不为我正在处理的项目制作一个新的 table 和带有种子数据的种子。当我 运行 迁移时我没有问题。我 运行 种子我没有问题,但是当我查看 PostgreSQL 数据库中的数据时,应该像所有其他种子一样不在其中。出于某种原因,它没有添加数据,我无法弄清楚为什么会这样。如果有人可以帮助我找到解决方案,那就太好了。
下面的代码和屏幕截图:
- 迁移
exports.up = function (knex) {
return knex.schema.createTable('alliance', (table) => {
table.increments('alliance_id').primary()
table.string('alliance_name')
table.string('description')
table.integer('user_id')
table
.foreign('user_id')
.references('user_id')
.inTable('users')
.onDelete('cascade')
})
}
exports.down = function (knex) {
return knex.schema.dropTable('alliance')
}
- 种子
const alliances = require('../seed-data/alliance-data')
exports.seed = function (knex) {
return knex
.raw('TRUNCATE TABLE alliance RESTART IDENTITY CASCADE')
.then(function () {
return knex('alliance').insert(alliances)
})
}
- 种子数据
module.exports = [
{
alliance_id: 0,
alliance_name: 'test 1',
description: 'testing 123',
},
]
- Knex 配置
const path = require('path')
require('dotenv').config()
const {
DATABASE_URL = 'postgresql://postgres@localhost/postgres',
} = process.env
module.exports = {
development: {
client: 'postgresql',
connection: DATABASE_URL,
pool: { min: 0, max: 100 },
migrations: {
directory: path.join(__dirname, 'src', 'db', 'migrations'),
},
seeds: {
directory: path.join(__dirname, 'src', 'db', 'seeds'),
},
},
production: {
client: 'postgresql',
connection: DATABASE_URL,
pool: { min: 0, max: 100 },
migrations: {
directory: path.join(__dirname, 'src', 'db', 'migrations'),
},
seeds: {
directory: path.join(__dirname, 'src', 'db', 'seeds'),
},
},
test: {
client: 'sqlite3',
connection: {
filename: ':memory:',
},
migrations: {
directory: path.join(__dirname, 'src', 'db', 'migrations'),
},
seeds: {
directory: path.join(__dirname, 'src', 'db', 'seeds'),
},
useNullAsDefault: true,
},
}
Dbeaver 截图
此问题的解决方案是确保您的种子和迁移顺序正确。
所以我不得不为我正在处理的项目制作一个新的 table 和带有种子数据的种子。当我 运行 迁移时我没有问题。我 运行 种子我没有问题,但是当我查看 PostgreSQL 数据库中的数据时,应该像所有其他种子一样不在其中。出于某种原因,它没有添加数据,我无法弄清楚为什么会这样。如果有人可以帮助我找到解决方案,那就太好了。 下面的代码和屏幕截图:
- 迁移
exports.up = function (knex) { return knex.schema.createTable('alliance', (table) => { table.increments('alliance_id').primary() table.string('alliance_name') table.string('description') table.integer('user_id') table .foreign('user_id') .references('user_id') .inTable('users') .onDelete('cascade') }) } exports.down = function (knex) { return knex.schema.dropTable('alliance') }
- 种子
const alliances = require('../seed-data/alliance-data') exports.seed = function (knex) { return knex .raw('TRUNCATE TABLE alliance RESTART IDENTITY CASCADE') .then(function () { return knex('alliance').insert(alliances) }) }
- 种子数据
module.exports = [ { alliance_id: 0, alliance_name: 'test 1', description: 'testing 123', }, ]
- Knex 配置
const path = require('path') require('dotenv').config() const { DATABASE_URL = 'postgresql://postgres@localhost/postgres', } = process.env module.exports = { development: { client: 'postgresql', connection: DATABASE_URL, pool: { min: 0, max: 100 }, migrations: { directory: path.join(__dirname, 'src', 'db', 'migrations'), }, seeds: { directory: path.join(__dirname, 'src', 'db', 'seeds'), }, }, production: { client: 'postgresql', connection: DATABASE_URL, pool: { min: 0, max: 100 }, migrations: { directory: path.join(__dirname, 'src', 'db', 'migrations'), }, seeds: { directory: path.join(__dirname, 'src', 'db', 'seeds'), }, }, test: { client: 'sqlite3', connection: { filename: ':memory:', }, migrations: { directory: path.join(__dirname, 'src', 'db', 'migrations'), }, seeds: { directory: path.join(__dirname, 'src', 'db', 'seeds'), }, useNullAsDefault: true, }, }
Dbeaver 截图
此问题的解决方案是确保您的种子和迁移顺序正确。