Rails的迁移 - PG ::错误:错误:整数无效的输入语法:"" [英] Rails Migration - PG::Error: ERROR: invalid input syntax for integer: ""
问题描述
我想部署我code到Heroku和我得到的错误
- 执行(ALTER TABLE旅馆ALTER COLUMN图像类型整数使用(图片::整数))
PG ::错误:错误:整数无效的输入语法:
:ALTER TABLE小屋ALTER COLUMN图像类型整数使用(图片::整数)
耙中止!
和我的迁移
类ChangeDataTypeForLodgesImage< ActiveRecord的::迁移
高清变化
执行ALTER TABLE旅馆ALTER COLUMN图像类型整数使用(图片::整数)
结束
结束
这错误是告诉你,你已经在 lodges.image
列空字符串,你可以不投一个空字符串为整数。你必须更改列类型之前修复损坏的数据。此修复程序取决于你想要什么空字符串是;一种可能是将它们转换为空值:
执行%Q {
更新小屋
集图像= NULL
其中,图像=''
}
执行ALTER TABLE旅馆ALTER COLUMN图像类型整数使用(图片::整数)
也许你想空字符串的为0:
执行%Q {
更新小屋
设定图象='0'
其中,图像=''
}
执行ALTER TABLE旅馆ALTER COLUMN图像类型整数使用(图片::整数)
有可能是因为你不能转换为整数其他值,你就必须同样地清除它们。
I am trying to deploy my code to heroku and i get the error
-- execute("ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)")
PG::Error: ERROR: invalid input syntax for integer: ""
: ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)
rake aborted!
and my migration is
class ChangeDataTypeForLodgesImage < ActiveRecord::Migration
def change
execute "ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)"
end
end
That error is telling you that you have empty strings in the lodges.image
column and you cannot cast an empty string to an integer. You'll have to fix the broken data before changing the column type. The fix depends on what you want empty strings to be; one possibility would be to convert them to NULLs:
execute %q{
update lodges
set image = null
where image = ''
}
execute "ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)"
Or perhaps you want empty strings to be zeros:
execute %q{
update lodges
set image = '0'
where image = ''
}
execute "ALTER TABLE lodges ALTER COLUMN image TYPE integer USING (image::integer)"
There may be other values that you can't cast to integers, you'll have to clean them up similarly.
这篇关于Rails的迁移 - PG ::错误:错误:整数无效的输入语法:&QUOT;&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!