[英]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_name
在psql
接口中检查表定义。
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.