Rails在现有数据库上迁移 [英] Rails migrations over an existing database

查看:136
本文介绍了Rails在现有数据库上迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的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 NULL

however 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
end

How 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在现有数据库上迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆