Rails在现有数据库上迁移

编程入门 行业动态 更新时间:2024-10-27 13:20:47
本文介绍了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在现有数据库上迁移

本文发布于:2023-10-26 23:04:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!