Search code examples
javascriptsqlpostgresqlherokuknex.js

When I try to update my database with PGSQL on Heroku, I get a 22001 error


I am working on a project that uses KnexJS with PGSQL on Heroku. My issue is that when I update the signature_url field (text), I receive a PGSQL 22001 error (details listed below). The thing that is being updated into the signature_url field is a string created by the .toDataUrl() JavaScript method. This method returns a string that is 16,158 characters long. This all seems to work fine when using SQLite3 in development but breaks when I try to update via PGSQL on Heroku.

Here are my migrations:

exports.up = function(knex, Promise) {
  return knex.schema.createTable('contracts', function (table) {
    table.increments('id')
    table.integer('owner_id')
    table.integer('signee_id')
    table.string('contract_header')
    table.text('contract_desc')
    table.text('signature_url')
    table.string('date_signed')
    table.boolean('isSigned')
  })
  };

exports.down = function(knex, Promise) {
  return knex.schema.dropTable('contracts')
};

Here are my original seeds:

exports.seed = function(knex, Promise) {
  // Deletes ALL existing entries
  return knex('contracts').del()
    .then(function () {
      // Inserts seed entries
      return knex('contracts').insert([
        {owner_id: 1, signee_id: 2, contract_header: 'Rugby Coaching Position', contract_desc: 'Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Donec sollicitudin molestie malesuada.', signature_url: '', date_signed: '', isSigned: false},
        {owner_id: 1, signee_id: 2, contract_header: 'Arts Perfomance', contract_desc: 'Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Donec sollicitudin molestie malesuada.', signature_url: '', date_signed: '', isSigned: false},
        {owner_id: 2, signee_id: 1, contract_header: 'Field Trip', contract_desc: 'Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Donec sollicitudin molestie malesuada.', signature_url: '', date_signed: '', isSigned: false}
      ]);
    });
};

This is the object that is returned when checking the network on Dev Tools.

code:"22001"
file:"varchar.c"
length:99
line:"624"
name:"error"
routine:"varchar"
severity:"ERROR"

db.js

function signContract (id, signatureUrl) {
  return knex('contracts').where('id', id)
  .update({ signature_url: signatureUrl })
}

knexfile.js

module.exports = {

  development: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    }
  },

  staging: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user:     'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  },

  test: {
  client: 'sqlite3',
  connection: {
    filename: './dev.sqlite3'
  }
},

  production: {
    client: 'postgresql',
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  }

};

Solution

  • You are trying to insert data into a varchar column, but the length of the string exceeds the length of the column.

    You can either insert a shorter string or use ALTER TABLE to increase the length of the varchar column.