hol*_*den 6 migration postgresql types ruby-on-rails alter-table
我的Postgres数据库中有一个Varchar类型的列我想要整数...现在我想要更改它们,不幸的是这似乎不能使用我的rails迁移.
change_column :table1, :columnB, :integer
Run Code Online (Sandbox Code Playgroud)
这似乎输出这个SQL:
ALTER TABLE table1 ALTER COLUMN columnB TYPE integer
Run Code Online (Sandbox Code Playgroud)
所以我尝试这样做:
execute 'ALTER TABLE table1 ALTER COLUMN columnB TYPE integer USING CAST(columnB AS INTEGER)'
Run Code Online (Sandbox Code Playgroud)
但是在这个实例中,强制转换不起作用,因为某些列为空...
有任何想法吗?
错误:
PGError: ERROR: invalid input syntax for integer: ""
: ALTER TABLE table1 ALTER COLUMN columnB TYPE integer USING CAST(columnB AS INTEGER)
Run Code Online (Sandbox Code Playgroud)
Postgres v8.3
eal*_*ent 13
听起来问题是你的表中有空字符串.你需要处理那些,可能是一个case语句,例如:
execute %{ALTER TABLE "table1" ALTER COLUMN columnB TYPE integer USING CAST(CASE columnB WHEN '' THEN NULL ELSE columnB END AS INTEGER)}
更新:根据更新的问题完全重写.
归档时间: |
|
查看次数: |
6742 次 |
最近记录: |