我有一个小表格,其中某个字段包含类型为“character varying”。我试图将其更改为“Integer”,但会出现无法转换的错误。
是否有解决方法,或者我应该只需创建另一个表,并使用查询将记录导入它?
该字段仅包含整数值。
我有一个小表格,其中某个字段包含类型为“character varying”。我试图将其更改为“Integer”,但会出现无法转换的错误。
是否有解决方法,或者我应该只需创建另一个表,并使用查询将记录导入它?
该字段仅包含整数值。
在编程中,从text
或varchar
到integer
没有隐式(自动)转换(即您不能将varchar
传递给期望integer
的函数或将varchar
字段分配给integer
字段),因此您必须使用ALTER TABLE ... ALTER COLUMN ... TYPE ... USING指定显式转换:
ALTER TABLE the_table ALTER COLUMN col_name TYPE integer USING (col_name::integer);
ALTER TABLE the_table ALTER COLUMN col_name TYPE integer USING (trim(col_name)::integer);
在转换之前去除空格。
如果在psql
中运行该命令,应该从错误消息中清楚地看到这一点,但是PgAdmin-III可能没有显示完整的错误。以下是我在PostgreSQL 9.2上在psql
中测试它时发生的情况:
=> CREATE TABLE test( x varchar );
CREATE TABLE
=> insert into test(x) values ('14'), (' 42 ');
INSERT 0 2
=> ALTER TABLE test ALTER COLUMN x TYPE integer;
ERROR: column "x" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.
=> ALTER TABLE test ALTER COLUMN x TYPE integer USING (trim(x)::integer);
ALTER TABLE
感谢@muistooshort添加了USING
链接。
另请参见这个相关问题;它是关于Rails迁移的,但根本原因是相同的,答案适用。
如果错误仍然发生,则可能与列值无关,而与此列上的索引或列默认值失败的类型转换有关。必须在ALTER COLUMN之前删除索引,并在之后重新创建索引。应适当更改默认值。
这个对我有用。
将varchar列更改为int
change_column :table_name, :column_name, :integer
收到:
PG::DatatypeMismatch: ERROR: column "column_name" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.
改成
change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'
您可以这样做:
change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'
或者尝试这个:
change_column :table_name, :column_name, :integer, using: 'column_name::integer'
如果您对此话题感兴趣,请阅读本文:https://kolosek.com/rails-change-database-column
请试一下,这样肯定会起作用。
当编写Rails迁移以将字符串列转换为整数时,通常会使用以下命令:
change_column :table_name, :column_name, :integer
PG::DatatypeMismatch: ERROR: column "column_name" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.
change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'
以上内容将模仿其他数据库适配器的操作方式。如果您有非数字数据,则可能会出现意外结果(但毕竟您正在转换为整数)。
我遇到了相同的问题。 然后我意识到我在尝试更改的列中有一个默认的字符串值。 移除默认值后,错误消失了 :)
我遇到了同样的问题。我开始重置列的默认设置。
change_column :users, :column_name, :boolean, default: nil
change_column :users, :column_name, :integer, using: 'column_name::integer', default: 0, null: false
boolean
是不必要的。 - Martin Zinovsky如果您正在开发环境下工作(或是生产环境下需要备份数据),那么请先清除数据库字段中的数据,或者将其值设置为0。
UPDATE table_mame SET field_name= 0;
接下来运行以下查询,成功运行查询后,进行模式迁移,然后运行迁移脚本。
ALTER TABLE table_mame ALTER COLUMN field_name TYPE numeric(10,0) USING field_name::numeric;
我认为这会对你有所帮助。
UPDATE the_table SET col_name = replace(col_name, 'some_string', '');
regexp_replace(col_name, '[^0-9.]','','g')
这样的东西。但如果你想保留 NaN
、Inf
和 10E42
科学计数法,则需要使用更加复杂的方法。 - Craig Ringer如果您有一个包含空字符串或null值的列,您可能会遇到以下错误信息:
invalid input syntax for type integer: ""
使用coalesce和nullif将空字符串和null值转换为零值。
ALTER TABLE peopleGroup ALTER numberPeople TYPE INT USING (cast ( coalesce( nullif( trim(numberPeople), '' ), '0' ) as integer ))
SELECT
列表,而不是依赖于列序号位置。话虽如此,答案中给出的方法将保留列位置。 - Craig Ringer