【问题标题】:knex create table and insert data from arrayknex 创建表并从数组中插入数据
【发布时间】:2016-04-18 04:49:57
【问题描述】:

我在迁移文件中有以下代码:

import knex from 'knex'
import filterWordsArray from '../seeds/filter_words'
const tables = ['filter_words'];

export async function up(knex) {
  await knex.schema.createTable('filter_words', (t) => {
    t.uuid('id').primary().defaultTo(knex.raw('uuid_generate_v4()'));
    t.string('word');
  }).then(function () {
    return knex("filter_words").insert(
      filterWordsArray
    );
  })

  return Promise.resolve();
};

export async function down(knex) {
  for (let table of tables) {
    await knex.schema.dropTable(table);
  }
  return Promise.resolve();
};

包含filterWordsArray的文件有以下内容:

export const filterWordsArray = [
{ 
word: "something"
},
{ 
word: "rather"
},
{ 
word: "filter"
},
{ 
word: "another"
},
. . .
];

我运行 knex migrate:latest ,没有错误。

$> knex migrate:latest
Using environment: development
Batch 3 run: 1 migrations
.../migrations/23460417191457_add_filter_words.js
$>

但是当我查看数据库时,创建了表但没有插入数据。

所以这是一个 Postgresql 数据库;如果我要手动插入数据,我会去:

mydb=# insert into filter_words(word) values ('something');
mydb=# select * from filter_words;
                  id                  |   word
--------------------------------------+-----------
 2ab8f5d0-ce20-41c2-a0e9-49cbe1b4bd3b | something
(1 row)

请问我在这里错过了什么?

【问题讨论】:

    标签: knex.js jsx


    【解决方案1】:

    好的,我想出了一种使它起作用的方法,但不确定它是否是唯一/最好的方法。将导入更改为:

    import {filterWordsArray} from '../seeds/filter_words'
    

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多