Insert/Update PostGis Geometry with Sequelize ORM

我与影子孤独终老i 提交于 2019-12-04 06:48:15

It seems that Sequelize does not save the SRID when saving a GEOMETRY field (note, that it correctly saves the SRID on a GEOGRAPHY field). Then when you update that model in the future, the update will fail because it does not have a SRID set on the GEOMETRY field as expected in the model definition.

This isn't a desirable solution, but you can use Sequelize's beforeSave hook to always speciify the coordinates system to use.

myDatabase.define('user', {
  name: {
    type: Sequelize.STRING
  },
  geometry: {
    type: Sequelize.GEOMETRY('POINT', 4326)
  }
}, {
  hooks: {
    beforeSave: function(instance) {
      if (instance.geometry && !instance.geometry.crs) {
        instance.geometry.crs = {
          type: 'name',
          properties: {
            name: 'EPSG:4326'
          }
        };
      }
    }
  }
});

Declare the column type as: DataTypes.GEOMETRY('Point'),

Set the model attribute as:

{
  type: 'Point',
  coordinates: [ lat, long ],
  crs: { type: 'name', properties: { name: 'EPSG:4326'} }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!