我使用rails generate migrations命令在我的rails应用程序中创建了一个表。以下是该迁移文件:
class CreateListings < ActiveRecord::Migration
def change
create_table :listings do |t|
t.string :name
t.string :telephone
t.string :latitude
t.string :longitude
t.timestamps
end
end
end
然后我想把纬度和经度存储为整数,所以尝试运行:
rails generate migration changeColumnType
文件内容如下:
class ChangeColumnType < ActiveRecord::Migration
def up
#change latitude columntype from string to integertype
change_column :listings, :latitude, :integer
change_column :listings, :longitude, :integer
#change longitude columntype from string to integer type
end
def down
end
end
我原本期望改变列的类型,但是rake被中止了并出现了以下错误信息。我想知道为什么无法执行?我的应用程序使用postgresql数据库。
rake db:migrate
== ChangeColumnType: migrating ===============================================
-- change_column(:listings, :latitude, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: column "latitude" cannot be cast to type integer
: ALTER TABLE "listings" ALTER COLUMN "latitude" TYPE integer
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
注意:该表格没有数据。
谢谢。