我使用 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)
注意:该表没有数据。谢谢