我正在创建一个新的Rails 3.1应用程序。 我想让这个新的应用程序重用一个现有的数据库(它是由以前的rails 2应用程序创建的)。
我创建了新的应用程序定义模型重新使用数据库中的一些现有数据。
在开发和测试阶段,一切运行正常,因为它运行在一个干净的表数据库,但是当试图部署到生产我收到以下消息:
PGError:ERROR:关系users的列email已存在 * ** [err :: localhost]:ALTER TABLEusersADD COLUMNemailcharacter varying(255)DEFAULT''NOT NULL但是我在迁移时认为像是
class DeviseCreateUsers< ActiveRecord :: Migration def change change_table(:users)do | t | t.database_authenticatable:null => false t.recoverable t.rememberable t.trackable t.timestamps end end如何使db:migrate忽略已有的内容,只更改新的东西和/或新类型?
我在stackoverflow上看到类似的问题,但没有回答这个问题。感谢您的答复。
解决方案如果您使用现有的数据库,那么您不应尝试通过迁移覆盖它,应该复制schema.rb中的现有数据库,然后从那里向前迁移,只添加已更改的字段。
I am creating a new Rails 3.1 application. I would like this new application to reuses an existing database (which was created by a previous rails 2 application).
I created the new application defining models that reuses some of the existing data in the database.
In the development and test phase everything works fine since it runs on a clean sheet database, but when trying to deploy to production I get messages such as:
PGError: ERROR: column "email" of relation "users" already exists *** [err :: localhost] : ALTER TABLE "users" ADD COLUMN "email" character varying(255) DEFAULT '' NOT NULLhowever I have in my migration thinks like
class DeviseCreateUsers < ActiveRecord::Migration def change change_table(:users) do |t| t.database_authenticatable :null => false t.recoverable t.rememberable t.trackable t.timestamps end endHow can I make db:migrate ignore what already exist and only change the new things and/or new types?
I saw similar questions on stackoverflow, but none answering this question. Thanks for your answers.
解决方案If you are using an existing database then you shouldn't try and override it through migrations, you should replicate the existing database in schema.rb and then migrate forwards from there, only adding the fields that have changed.
更多推荐
Rails在现有数据库上迁移
发布评论