Sequelize GEOMETRY type field

Asked

Viewed 124 times

-1

Hello, I wanted to create a table using Migrations with sequelize, and create a position field of type GEOMETRY. The bank I’m using is the postgres.

I tried that code:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize, DataTypes) => {
    return queryInterface.createTable('users', { 
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      name: {
        type: Sequelize.STRING,
      },
      position: {
        type: Sequelize.GEOMETRY,
      },
    });
  },

  down: queryInterface => {
    return queryInterface.dropTable('users');
  }
};

Gives an error stating that type "Geometry" does not exist.

How do I set a field like this??

  • You have activated the extension postgis in your bank?

  • I don’t remember, but I don’t think so. How do I do it?

  • from the command line I think it’s like: psql -U [superuser] [sua database] CREATE EXTENSION postgis;. I’m not sure, but try the test. If not, do a search of how to activate this extension, for the postgres to recognize this multidimensional type of data.

  • I’ll try, as soon as I return result

  • is enabled but keeps giving error that is on top

1 answer

0

then, from what I saw in the source code, put in your type the following:

Sequelize.GEOMETRY('POINT') // letras em caixa alta.

This will indicate a 2-dimensional geometry.
Remembering that as the documentation of the sequel show, Voce can choose 3 types: Point, LineString and Polygon.

When you create a new record in the bank, you must inform the type when creating. Example:

const point = { type: 'Point', coordinates: [39.807222,-76.984722]}; // especifica o type para `Point`.

User.create({username: 'username', position: point });

I guess that’s it. I hope I helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.