使用objectionjs插入/选择mysql空间列

czq61nw1  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(308)

我在mysql中有一个位置表,其中有一个'point'类型的列。我可以用knex定义它,而且它工作正常。

knex.schema.createTable('locations',function(table){
  table.increments('locid').primary();
  table.string('address').notNullable();
  table.string('nick_name');
  table.specificType('loc_gis','Point').nullable();
  table.enu('loc_type',['home','office','college','other']).notNullable();
  table.timestamps();
})

但是我不知道如何使用objectionjs选择/插入gis空间数据。我是否必须每次都使用原始查询,或者无论如何都要为objectionjs编写一个插件来处理空间列。

drkbr07n

drkbr07n1#

找到了答案。下面的代码在我的objectionjs模型定义中修复了这个问题。

$formatDatabaseJson(json) {
 // Remember to call the super class's implementation.
  if(json.locGis){
    json.locGis=knex.raw('point(?, ?)', 
      [json.locGis.x, json.locGis.y]);
  }
  json = super.$formatDatabaseJson(json);
  return json;
}

相关问题