首页 > 解决方案 > 使用多边形列对批量插入错误进行续集

问题描述

我有以下模型,其中有一列多边形

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('p_zones', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      points: {
        type: Sequelize.GEOMETRY('POLYGON')
      },
    });
  },
  down: async (queryInterface) => {
    await queryInterface.dropTable('ZonePoints');
  }
};

当我尝试使用带有命令的 sequelize-cli 播种时npx sequelize-cli db:seed:all

const polygon = {
      type: 'Polygon',
      coordinates: [
        [
          [10, 10],
          [20, 10],
          [20, 20],
          [10, 20],
          [10, 10],
        ],
      ],
    };
 const data = [{ points: polygon }];
 await queryInterface.bulkInsert('p_zones', data);

我收到以下错误:

ERROR: Invalid value {
  type: 'Polygon',
  coordinates: [ [ [ 10, 10 ], [ 20, 10 ], [ 20, 20 ], [ 10, 20 ], [ 10, 10 ] ] ]
}

我究竟做错了什么?

标签: javascriptmariadbsequelize.jssequelize-cli

解决方案


对于版本 6 ,这似乎是github 上的一个未解决问题Sequelize。您可以尝试Americas建议的内容GitHub,即

await queryInterface.bulkInsert(
    'p_zones',
    data,
    {},
    { points: { type: Sequelize.GEOMETRY('POLYGON') } });

postgres虽然我必须承认,当我对数据库执行上述操作时,出现以下错误:

Executing (default): INSERT INTO "p_zones" ("points") VALUES (GeomFromText('POLYGON((10 10,20 10,20 20,10 20,10 10))'));
(node:18964) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: function geomfromtext(unknown) does not exist
    at Query.formatError ...

但希望这只是一个错误,PostGIS因为我拥有的特定版本。

顺便说一句,如果它是一个.bulkCreate语句而不是一个语句,则您的插入可以正常工作.bulkInsert.bulkInsert不幸的是,如果不先定义整个模型,我不知道如何执行 a 。你可以尝试的事情:

  1. 构造一个原始查询并以这种方式进行批量插入。
  2. 尝试通过sequelize从 访问对象来定义模型queryInterface.sequelize,然后执行 a .bulkCreate,这不会给您任何错误。

推荐阅读