Search code examples
javascriptpostgresqlmigrationpsqlknex.js

How to seed latitude and longitude from Knex seed data to PSQL?


I am using leaflet to create a map-based project. I have set up a database on PostgreSQL and using Knex for migration.

I have created a series of seed data, the last Knex seed is for added map events to a table, with one of the column type being POINT in PSQL. However, when I try to add the latitude and longitude on Knex, it wouldn't allow me to push to the table. Any ideas how to post the lat/lng to the 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},
    ]);
};

And this is the table that was migrated up:

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");
        })

And this was the error that got throw out:

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, $1, $2, $3, $4) - 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, $1, $2, $3, $4) - 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.

Solution

  • Putting it down on paper for future reference.

    To resolve the problem, in the knex seed file, put in knex.raw('POINT (x, y)') to add coordinates into PostgreSQL.