PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to seed latitude and longitude from Knex seed data to PSQL?

 April 13, 2022     javascript, knex.js, migration, postgresql, psql     No comments   

Issue

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.



Answered By - Beaumont
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing