Knex 在 Postgres 中创建 POINT 数据类型
Knex create POINT data type in Postgres
我正在尝试创建一个具有 POINT 数据类型的架构,而 knex 似乎并没有在数据库中创建它。但是它正在创建所有其他字段。
这是我的迁移文件的样子:
exports.up = (knex, Promise) => {
return Promise.all([
knex.schema.createTableIfNotExists('users', (table) => {
table.uuid('id').primary()
table.string('username', 35)
table.text('pword').notNullable()
table.string('first_name', 55)
table.string('last_name', 55)
knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
table.timestamp('date_created').defaultTo(knex.fn.now())
})
])
}
exports.down = (knex, Promise) => {
return Promise.all([
knex.schema.dropTableIfExists('users')
])
}
这是失败的代码行:
knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
我也试过删除 schema 属性:
knex.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
没有打印出任何错误,似乎只是默默地失败了。
编辑 1:
我使用以下方法打印出来:knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)').then(data => console.log(data)).catch(error => console.log(error))
{ error: coordinates POINT DEFAULT POINT (37.3875, -122.0575) - syntax error at or near "coordinates"
at Connection.parseE (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:381:17)
at Socket.<anonymous> (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:117:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:172:18)
at Socket.Readable.push (_stream_readable.js:130:10)
at TCP.onread (net.js:542:20)
name: 'error',
length: 92,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '1',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1081',
routine: 'scanner_yyerror' }
试试这个:
table.specificType('coordinates', 'POINT').defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
对于 Knex 未涵盖的 SQL 类型,您始终可以使用以下格式:
table.specificType('column_name', 'TYPE')
我正在尝试创建一个具有 POINT 数据类型的架构,而 knex 似乎并没有在数据库中创建它。但是它正在创建所有其他字段。
这是我的迁移文件的样子:
exports.up = (knex, Promise) => {
return Promise.all([
knex.schema.createTableIfNotExists('users', (table) => {
table.uuid('id').primary()
table.string('username', 35)
table.text('pword').notNullable()
table.string('first_name', 55)
table.string('last_name', 55)
knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
table.timestamp('date_created').defaultTo(knex.fn.now())
})
])
}
exports.down = (knex, Promise) => {
return Promise.all([
knex.schema.dropTableIfExists('users')
])
}
这是失败的代码行:
knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
我也试过删除 schema 属性:
knex.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)')
没有打印出任何错误,似乎只是默默地失败了。
编辑 1:
我使用以下方法打印出来:knex.schema.raw('coordinates POINT DEFAULT POINT (37.3875, -122.0575)').then(data => console.log(data)).catch(error => console.log(error))
{ error: coordinates POINT DEFAULT POINT (37.3875, -122.0575) - syntax error at or near "coordinates"
at Connection.parseE (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:381:17)
at Socket.<anonymous> (/Users/james/plural/backend-development/node_modules/pg/lib/connection.js:117:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:172:18)
at Socket.Readable.push (_stream_readable.js:130:10)
at TCP.onread (net.js:542:20)
name: 'error',
length: 92,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '1',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1081',
routine: 'scanner_yyerror' }
试试这个:
table.specificType('coordinates', 'POINT').defaultTo(knex.raw('POINT (37.3875, -122.0575)'))
对于 Knex 未涵盖的 SQL 类型,您始终可以使用以下格式:
table.specificType('column_name', 'TYPE')