当我尝试使用Heroku上的PGSQL更新我的数据库时,出现22001错误

时间:2017-07-10 23:00:37

标签: javascript sql postgresql heroku knex.js

我正在开发一个在Heroku上使用KnexJS和PGSQL的项目。我的问题是,当我更新signature_url字段(文本)时,我收到PGSQL 22001错误(详情如下)。正在更新到signature_url字段的内容是由.toDataUrl()JavaScript方法创建的字符串。此方法返回一个长度为16,158个字符的字符串。在开发中使用SQLite3时,这一切似乎都可以正常工作,但是当我尝试通过Heroku上的PGSQL进行更新时,这一切都会中断。

以下是我的迁移:

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

这是我的原始种子:

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

这是在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'
    }
  }

};

3 个答案:

答案 0 :(得分:1)

您正在尝试将数据插入varchar列,但字符串的长度超出了列的长度。

您可以插入较短的字符串,也可以使用ALTER TABLE来增加varchar列的长度。

答案 1 :(得分:0)

尝试检查列的类型。因为您的错误表明您正试图在小类型中加入大值。在psql界面中使用\d table_name检查您的表格定义。

如果您看到非text列 - 请尝试将其替换为

ALTER TABLE tbl_name ALTER COLUMN col_name TYPE text;

我更喜欢使用text字段

UPD:并尝试检查应用程序内的NODE_ENV变量。也许您已连接到sqlite数据库。我尝试在我当地的postgres上重现你的情况,一切正常。

PS 为什么不在开发/测试环境中使用postgres?例如,您的应用中只需要一些postgres - 仅限功能,这意味着您无法在开发中测试您的应用。但这超出了这个问题。

答案 2 :(得分:0)

为你的帮助人们欢呼!我已经采纳了您的想法,并通过声明最大字符长度table.string("signature_url")的值来修改我的table.string("signature_url", 17000)列,将其应用于KnexJS。这似乎有用了!