如何在 knex 模式中指定浮点精度?
How to specify float precision in knex schema ?
我在 knex 模式中定义浮点精度时遇到问题,我希望它是一个浮点数 (10,6) 以便存储 lat/long 位置。这是我尝试声明它的方式:
lat: {type: 'float(10,6)', nullable: false},
由于 (10,6) 的原因,它在迁移时失败了,那么正确的方法是什么?
我不知道这种 JSON 方法,因为我只使用 Schema Builder 一种方法。
它看起来像(例如 geostuff
table 和 id
、lat
和 lng
列:
var knex = require('knex')({client:'sqlite3',connection:{filename: 'sample.sqlite3'}});
knex.schema.createTable('geostuff', function(table) {
table.increments('id').primary();
table.float('lat', 14, 10).notNullable();
table.float('lng', 14, 10).notNullable();
}).then(function() {
console.dir('table created');
}).catch(function(err) {
console.log('table not created');
console.dir(err);
});
请注意,精度说明符并未得到广泛支持。 PostgreSQL 支持 float(precision)
,但 SQLite3(来自示例)不支持(更好的是:不关心)。幸运的是 knex.js 隐藏了那些特质。
为了存储地理位置,我使用了双精度数据类型,为此我的迁移保持这样:
exports.up = function(knex, Promise) {
return knex.schema.createTable('route', table => {
table.increments('id').primary()
table.specificType('latitude', 'double precision').notNull()
table.specificType('longitude', 'double precision').notNull()
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('route')
};
我在 knex 模式中定义浮点精度时遇到问题,我希望它是一个浮点数 (10,6) 以便存储 lat/long 位置。这是我尝试声明它的方式:
lat: {type: 'float(10,6)', nullable: false},
由于 (10,6) 的原因,它在迁移时失败了,那么正确的方法是什么?
我不知道这种 JSON 方法,因为我只使用 Schema Builder 一种方法。
它看起来像(例如 geostuff
table 和 id
、lat
和 lng
列:
var knex = require('knex')({client:'sqlite3',connection:{filename: 'sample.sqlite3'}});
knex.schema.createTable('geostuff', function(table) {
table.increments('id').primary();
table.float('lat', 14, 10).notNullable();
table.float('lng', 14, 10).notNullable();
}).then(function() {
console.dir('table created');
}).catch(function(err) {
console.log('table not created');
console.dir(err);
});
请注意,精度说明符并未得到广泛支持。 PostgreSQL 支持 float(precision)
,但 SQLite3(来自示例)不支持(更好的是:不关心)。幸运的是 knex.js 隐藏了那些特质。
为了存储地理位置,我使用了双精度数据类型,为此我的迁移保持这样:
exports.up = function(knex, Promise) {
return knex.schema.createTable('route', table => {
table.increments('id').primary()
table.specificType('latitude', 'double precision').notNull()
table.specificType('longitude', 'double precision').notNull()
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('route')
};