I have the following model with a column of type polygon
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');
}
};
And when I try to seed using sequelize-cli with the command 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);
I get the following error:
ERROR: Invalid value {
type: 'Polygon',
coordinates: [ [ [ 10, 10 ], [ 20, 10 ], [ 20, 20 ], [ 10, 20 ], [ 10, 10 ] ] ]
}
What am I doing wrong?
This appears to be an open issue on github for Sequelize
version 6. You could try what Americas
suggests on GitHub
, i.e.
await queryInterface.bulkInsert(
'p_zones',
data,
{},
{ points: { type: Sequelize.GEOMETRY('POLYGON') } });
Although I must admit, when I executed the above against a postgres
database, I got the following error:
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 ...
But hopefully that's just an error due to the particular version of PostGIS
that I have.
As an aside, your insert works fine if it's a .bulkCreate
statement rather than a .bulkInsert
one. Unfortunately, I don't know how to execute a .bulkInsert
without defining the entire model first. Things you could try:
sequelize
object from queryInterface.sequelize
, and then do a .bulkCreate
, which shouldn't give you any errors.