如何将 Knex 种子数据的经纬度播种到 PSQL?
How to seed latitude and longitude from Knex seed data to PSQL?
我正在使用 leaflet 创建一个基于地图的项目。我已经在 PostgreSQL 上建立了一个数据库并使用 Knex 进行迁移。
我创建了一系列种子数据,最后一个 Knex 种子用于将地图事件添加到 table,其中一个列类型是 PSQL 中的 POINT。但是,当我尝试在 Knex 上添加经纬度时,它不允许我推送到 table。关于如何将 post lat/lng 变为 table 的任何想法?
import { Knex } from "knex";
export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex("events").del();
// Inserts seed entries
await knex("events").insert([
{ eventlocation: {x: 22.28052, y: 114.15749}, eventname: "party", description: "party at LKF 10pm", date: knex.fn.now(), userid: 1},
]);
};
这是向上迁移的table:
const events = await knex.schema.hasTable('events');
if (!events)
await knex.schema.createTable('events', (table) => {
table.increments("eventid");
table.specificType("eventlocation", 'POINT');
table.string("eventname");
table.string("description", 255);
table.timestamps(false,true);
table.integer("userid").unsigned().references("id").inTable("users");
})
这是被抛出的错误:
Error: Error while executing "/Users/beaumont/Desktop/lectures/project3/seeds/4-create-events.ts" seed: insert into "events" ("created_at", "description", "eventlocation", "eventname", "userid") values (CURRENT_TIMESTAMP, , , , ) - invalid input syntax for type point: "{"x":22.28052,"y":114.15749}"
at Seeder._waterfallBatch (/Users/beaumont/Desktop/lectures/project3/node_modules/knex/lib/migrations/seed/Seeder.js:146:23)
error: insert into "events" ("created_at", "description", "eventlocation", "eventname", "userid") values (CURRENT_TIMESTAMP, , , , ) - invalid input syntax for type point: "{"x":22.28052,"y":114.15749}"
at Parser.parseErrorMessage (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:378:20)
at Socket.EventEmitter.emit (node:domain:470:12)
at addChunk (node:internal/streams/readable:313:12)
at readableAddChunk (node:internal/streams/readable:288:9)
at Socket.Readable.push (node:internal/streams/readable:227:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
记在纸上以备将来参考。
为了解决这个问题,在knex种子文件中,输入knex.raw('POINT (x, y)')
以将坐标添加到PostgreSQL中。
我正在使用 leaflet 创建一个基于地图的项目。我已经在 PostgreSQL 上建立了一个数据库并使用 Knex 进行迁移。
我创建了一系列种子数据,最后一个 Knex 种子用于将地图事件添加到 table,其中一个列类型是 PSQL 中的 POINT。但是,当我尝试在 Knex 上添加经纬度时,它不允许我推送到 table。关于如何将 post lat/lng 变为 table 的任何想法?
import { Knex } from "knex";
export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex("events").del();
// Inserts seed entries
await knex("events").insert([
{ eventlocation: {x: 22.28052, y: 114.15749}, eventname: "party", description: "party at LKF 10pm", date: knex.fn.now(), userid: 1},
]);
};
这是向上迁移的table:
const events = await knex.schema.hasTable('events');
if (!events)
await knex.schema.createTable('events', (table) => {
table.increments("eventid");
table.specificType("eventlocation", 'POINT');
table.string("eventname");
table.string("description", 255);
table.timestamps(false,true);
table.integer("userid").unsigned().references("id").inTable("users");
})
这是被抛出的错误:
Error: Error while executing "/Users/beaumont/Desktop/lectures/project3/seeds/4-create-events.ts" seed: insert into "events" ("created_at", "description", "eventlocation", "eventname", "userid") values (CURRENT_TIMESTAMP, , , , ) - invalid input syntax for type point: "{"x":22.28052,"y":114.15749}"
at Seeder._waterfallBatch (/Users/beaumont/Desktop/lectures/project3/node_modules/knex/lib/migrations/seed/Seeder.js:146:23)
error: insert into "events" ("created_at", "description", "eventlocation", "eventname", "userid") values (CURRENT_TIMESTAMP, , , , ) - invalid input syntax for type point: "{"x":22.28052,"y":114.15749}"
at Parser.parseErrorMessage (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/Users/beaumont/Desktop/lectures/project3/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:378:20)
at Socket.EventEmitter.emit (node:domain:470:12)
at addChunk (node:internal/streams/readable:313:12)
at readableAddChunk (node:internal/streams/readable:288:9)
at Socket.Readable.push (node:internal/streams/readable:227:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
记在纸上以备将来参考。
为了解决这个问题,在knex种子文件中,输入knex.raw('POINT (x, y)')
以将坐标添加到PostgreSQL中。