Search code examples
sails.jssails-postgresql

How to use bigserial for primary key id with Sails js


I have the following model definition:

module.exports = {
  attributes: {
    id: {
    type: 'bigserial',
    primaryKey: true
  },
  email: {
    type: 'email',
    required: true,
    unique: true
  }
}

When I lifed sails, it didn't give me warning about "bigserial" data type, even though it is not officially documented.

However, on the table created on Postgresql, the column "id" has a type "text". How do I have bigserial primary key?


Solution

  • You can use this command in your DB `ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY;

    In Sails model you can use somethin like this:

     module.exports = {
      connection: yourConnection,
      tableName: yourTableName,
      attributes: {
        id: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
      },
      email: {
        type: 'email',
        required: true,
        unique: true
      }
    }
    

    Note: you need to set your db connection config previously on config/connections.js

      somePostgresqlServer: {
        adapter: 'sails-postgresql',
        host: 'YOUR_POSTGRES_SERVER_HOSTNAME_OR_IP_ADDRESS',
        user: 'YOUR_POSTGRES_USER',
        password: 'YOUR_POSTGRES_PASSWORD',
        database: 'YOUR_POSTGRES_DB'
      }
    

    and run npm install sails-postgresqlin your project folder