简体   繁体   English

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

[英]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. 我正在研究在Heroku上将KnexJS与PGSQL一起使用的项目。 My issue is that when I update the signature_url field (text), I receive a PGSQL 22001 error (details listed below). 我的问题是,当我更新signature_url字段(文本)时,我收到PGSQL 22001错误(下面列出的详细信息)。 The thing that is being updated into the signature_url field is a string created by the .toDataUrl() JavaScript method. 正在更新到signature_url字段中的是由.toDataUrl()JavaScript方法创建的字符串。 This method returns a string that is 16,158 characters long. 此方法返回一个长16,158个字符的字符串。 This all seems to work fine when using SQLite3 in development but breaks when I try to update via PGSQL on Heroku. 当在开发中使用SQLite3时,这一切似乎都可以正常工作,但是当我尝试通过Heroku上的PGSQL进行更新时,这一切都会中断。

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 db.js

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

knexfile.js 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'
    }
  }

};

You are trying to insert data into a varchar column, but the length of the string exceeds the length of the column. 您正在尝试将数据插入varchar列,但是字符串的长度超过了该列的长度。

You can either insert a shorter string or use ALTER TABLE to increase the length of the varchar column. 您可以插入较短的字符串,也可以使用ALTER TABLE来增加varchar列的长度。

Try to check your types for your columns. 尝试检查您的列的类型。 Because your error states that you are trying to put big value inside small type. 因为您的错误表明您正在尝试将大值放入小类型中。 Check your table definition in psql interface with \\d table_name . 使用\\d table_namepsql接口中检查表定义。

If you see non- text columns – try to replace them with 如果看到非text列,请尝试将其替换为

ALTER TABLE tbl_name ALTER COLUMN col_name TYPE text;

I prefer using text fields 我更喜欢使用文本字段

UPD: And try to check your NODE_ENV variable inside of your application. UPD:并尝试在应用程序中检查您的NODE_ENV变量。 Maybe you are connected to sqlite database. 也许您已连接到sqlite数据库。 I tried to reproduce your situation on my local postgres and everything works just fine. 我试图在我当地的postgres上重现您的情况,并且一切正常。

PS And why don't you use postgres for development/test environments? PS并且为什么不在开发/测试环境中使用postgres? For example, you'll need some postgres -only features in your app, so that means you cannot test your app in development. 例如,您的应用程序中将需要仅使用postgres功能,因此这意味着您无法在开发中测试您的应用程序。 But this goes beyond this question. 但这超出了这个问题。

Cheers for your help people! 为您的帮助人们加油! I've taken your ideas and applied it to KnexJS by modifying my table.string("signature_url") column by declaring a value for the maximum character length table.string("signature_url", 17000) . 我已经采纳了您的想法,并通过声明最大字符长度table.string("signature_url", 17000)的值来修改table.string("signature_url")列,将其table.string("signature_url", 17000) This seems to of worked! 这似乎行之有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试测试运行我的 MySQL 数据库时收到此错误消息 - I get this error message when I try to test run my MySQL database 如何解决错误“代码”:postgresql 中的“22001” - How do I resolve the error "code": "22001" in postgresql 当我尝试连接到我的数据库 nodejs 时,我收到一个未知的数据库错误 - Im getting a unknown database error when i try to connect to my database nodejs 当我尝试使用npm我安装我的项目包时出现错误 - I get an error when I try to install my project packages with npm i 当我尝试解析json时出错 - Error when I try to parse my json 部署到heroku时,我的应用程序不断崩溃。 我收到这个担心的应用程序错误主屏幕 - My app keeps crashing when i deploy to heroku. i get this wried app error home screen 当我尝试添加 API 密钥时,出现错误:TypeError: items.map is not a function - I get the error: TypeError: items.map is not a function, when I try to add my API key UnhandledPromiseRejectionWarning:错误:当我尝试运行我的代码时找不到模块“../database/db” - UnhandledPromiseRejectionWarning: Error: Cannot find module '../database/db' when i try to run my code 当我尝试在函数中添加 if 时,为什么会出现错误“无法访问的代码”? - Why i get error "unreachable code" when i try to put an if in my function? 当我尝试使用cropperJS获得50%的图像时出现错误 - I get the error when i try get 50% of the image with cropperJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM