如何使用node.js、db-migrate、postgres插入多行

问题描述 投票:0回答:3

我有一个角色迁移。

'use strict';

var dbm;
var type;
var seed;

/**
  * We receive the dbmigrate dependency from dbmigrate initially.
  * This enables us to not have to rely on NODE_PATH.
  */
exports.setup = function (options, seedLink) {
  dbm = options.dbmigrate;
  type = dbm.dataType;
  seed = seedLink;
};

exports.up = function (db) {
  return db.createTable('roles', {
    role_id: { type: 'int', primaryKey: true, unique: true, autoIncrement: true },
    role_name: 'text',
  })
}

exports.down = function (db) {
  return db.dropTable('roles')
};

exports._meta = {
  "version": 1
};

现在,我想设置一些默认角色。我该怎么做?

'use strict';

var dbm;
var type;
var seed;

/**
  * We receive the dbmigrate dependency from dbmigrate initially.
  * This enables us to not have to rely on NODE_PATH.
  */

exports.setup = function (options, seedLink) {
  dbm = options.dbmigrate;
  type = dbm.dataType;
  seed = seedLink;
};

exports.up = function (db) {
  const sql = "DELETE FROM roles"
  db.runSql(sql, function (err) {
    if (err) return console.log(err)
  })

  return db.insert(
    'roles',
    ['user_role'],
    [['admin', 'technician']]
  )
};

exports.down = function (db) {
  const sql = "DELETE FROM roles"
  db.runSql(sql, function (err) {
    if (err) return console.log(err)
  })
  return null
};

exports._meta = {
  "version": 1
};

无论我尝试什么,我总能得到

“'admin);”处或附近的未终止引号字符串

作为一个错误。

它在 docker 容器中运行,我已经运行了

run migrate_up
进行两次迁移

文档说明我需要这样做:

insert(tableName, columnNameArray, valueArray, callback)

那么,如何在一个语句中添加多行?

node.js postgresql insert migration dbmigrate
3个回答
0
投票

我有类似的问题,我设法将它们一一插入,将它们的承诺推入数组,然后对它们调用全部承诺。所以在你的情况下。

exports.up = function (db) {
  const sql = "DELETE FROM roles"
  db.runSql(sql, function (err) {
    if (err) return console.log(err)
  })
  

  const promises = [];

  promises.push(db.insert('roles', { user_role : 'admin'}))
  promises.push(db.insert('roles', { user_role : 'technician'}))

  return Promise.all(promises)
};

0
投票

这就是我最终所做的。希望它对其他人有帮助

'use strict'

var dbm
var type
var seed

/**
 * We receive the dbmigrate dependency from dbmigrate initially.
 * This enables us to not have to rely on NODE_PATH.
 */
exports.setup = function (options, seedLink) {
  dbm = options.dbmigrate
  type = dbm.dataType
  seed = seedLink
}

exports.up = function (db) {
  // If there are roles, we need to delete them anyway
  const sql = 'DELETE FROM roles'
  db.runSql(sql, function (err) {
    if (err) return console.log(err)
  })

  return db.insert('roles', ['role_name'], ['admin']).then(
    function (result) {
      return db.insert('roles', ['role_name'], ['technician'])
    },
    function (err) {
      if (err) return console.log(err)
    },
  )
}

exports.down = function (db) {
  const sql = 'DELETE FROM roles'
  db.runSql(sql, function (err) {
    if (err) return console.log(err)
  })
  return null
}

exports._meta = {
  version: 1,
}

0
投票

以下代码对我有用。(对@Rassix的答案做了一些小修改)

exports.up = function (db) {
  const promises = [];
  promises.push(db.insert('roles', ['user_role'] , ['Admin']));
  promises.push(db.insert('roles', ['user_role'] ,['Technician']));
  return Promise.all(promises);
};
© www.soinside.com 2019 - 2024. All rights reserved.