2

I managed to create a model/schema and insert geo-Points into Postgis using sequelize. Since I have a lot of points (>100K) I am longing back to the old way I used to import, using ogr2ogr (gdal), which was much faster (almost instant instead of >20 minutes). As I would like to continue to work with sequelize after the initial import I still want sequelize to create the model/schema upfront, but then do import with ogr2ogr, and then continue CRUD with sequelize.

Here I found this fragment “[….] One way to get around this is to create your table structures before hand and use OGR2OGR to just load the data.” Which gave me the idea that this could work for Postgres/Postgis as well.

Sequelize creates timestamp columns for createdAt and updatedAt, which I like. But when I use ogr2ogr I get “null value in column "createdAt" violates not-null constraint” as loginfo.

Based on this slightly similar issue I tried to add a createdAt column by adding an -sql option:

ogr2ogr -f PostgreSQL PG:"host='0.0.0.0' user='root' port='5432' dbname='db' password='pw'" /home/user/geojsonImportfile.json -nln "DataPoints" -a_srs EPSG:4326 -sql "SELECT url, customfield, wkb_geometry, CURRENT_TIMESTAMP as createdAt FROM '/home/usr/geojsonImportfile.json'" -dialect 'PostgreSQL'

The error I get when running this is:

ERROR 1: SQL Expression Parsing Error: syntax error, unexpected end of string, expecting '.'. Occurred around :
home/user/geojsonImportfile0.json'

Besides my lack of SQL knowledge I am not sure if this can work at all.

How can I solve this, i.e. make the import with ogr2ogr but keep the timestamp columns?

4

2 回答 2

2

当您使用 和 创建表时,sequelize.define列会自动创建为。createdAtupdatedAttimestamp with time zone NOT NULL

但是您可以在 sequelize 定义脚本中规定非空约束:

const Mytable = sequelize.define('mytable', {
    id: {type: Sequelize.INTEGER, primaryKey: true},
    createdAt: {type: Sequelize.DATE, validate: {notNull:false}}
});

然后创建表,如下所示:

CREATE TABLE mytables
(
  id integer NOT NULL,
  "createdAt" timestamp with time zone,
  "updatedAt" timestamp with time zone NOT NULL,
  CONSTRAINT mytables_pkey PRIMARY KEY (id)
)

http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration

于 2017-07-13T18:50:52.687 回答
1

@JGH 按照您的建议,使用默认时间戳是有意义的。我已经可以使用 sequelize 进行设置,如下所述

var user = sequelize.define('user', {
  createdAt: {
    type: DataTypes.DATE,
    field: 'beginTime',
    defaultValue: sequelize.literal('NOW()')
  }
}, {
  timestamps: true,

});
于 2017-07-13T18:52:14.723 回答