首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails:表已经存在于新的git分支上

rails:表已经存在于新的git分支上
EN

Stack Overflow用户
提问于 2012-03-02 05:03:27
回答 2查看 1.1K关注 0票数 0

我试着玩不同的信息宝石,所有使用的设计。因此,在git的主分支上安装了Devise之后,我检查了一个新的分支来尝试一个创业板"rails消息传递“https://github.com/frodefi/rails-messaging,当我无法让它工作时,我提交了对该分支的更改,然后我返回到master并检查了一个新的分支来尝试另一个创业板"mailboxer”https://github.com/ging/mailboxer/tree/master/lib。在这个新分支的宝石文件中,没有第一个分支中的gems的踪迹,但是当我在安装gems之后尝试rake db:迁移到这个第二个分支时,我收到了以下错误消息,这似乎表明来自第一个分支的表正在干扰第二个分支的rake,除非执行以下操作。

代码语言:javascript
复制
 rails g mailboxer:install

它会自动运行rake :migrate。然而,README说我必须运行rake db:迁移,所以我不确定.

代码语言:javascript
复制
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "conversations" already exists: CREATE TABLE "conversations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject" varchar(255) DEFAULT '', "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL

)

有人能建议我怎么解决这个问题吗?我真的不知道要运行哪些命令来解决这个问题。

这是scheme.rb文件。基于“消息传递用户”索引,我猜想这是rails消息传递创业板(在第一个分支上)创建的设置,但我不完全确定。我看了git上的源代码,但因为我经验不多,所以有点迷路了。

代码语言:javascript
复制
ActiveRecord::Schema.define(:version => 20120302041333) do

  create_table "conversations", :force => true do |t|
    t.string   "subject",    :default => ""
    t.datetime "created_at",                 :null => false
    t.datetime "updated_at",                 :null => false
  end

  create_table "messaging_users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                         :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "messaging_users", ["email"], :name => "index_messaging_users_on_email", :unique => true
  add_index "messaging_users", ["reset_password_token"], :name => "index_messaging_users_on_reset_password_token", :unique => true

  create_table "notifications", :force => true do |t|
    t.string   "type"
    t.text     "body"
    t.string   "subject",              :default => ""
    t.integer  "sender_id"
    t.string   "sender_type"
    t.integer  "conversation_id"
    t.boolean  "draft",                :default => false
    t.datetime "updated_at",                              :null => false
    t.datetime "created_at",                              :null => false
    t.integer  "notified_object_id"
    t.string   "notified_object_type"
    t.string   "notification_code"
  end

  add_index "notifications", ["conversation_id"], :name => "index_notifications_on_conversation_id"

  create_table "receipts", :force => true do |t|
    t.integer  "receiver_id"
    t.string   "receiver_type"
    t.integer  "notification_id",                                  :null => false
    t.boolean  "read",                          :default => false
    t.boolean  "trashed",                       :default => false
    t.boolean  "deleted",                       :default => false
    t.string   "mailbox_type",    :limit => 25
    t.datetime "created_at",                                       :null => false
    t.datetime "updated_at",                                       :null => false
  end

  add_index "receipts", ["notification_id"], :name => "index_receipts_on_notification_id"

  create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                         :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end
EN

回答 2

Stack Overflow用户

发布于 2012-03-02 05:13:10

这里的问题是,虽然您的代码、模式、宝石和迁移存储在git中,但数据库本身却不是。因此,当您在分支之间切换时,数据库仍然保留其状态。

这些解决办法都将有效:

  1. 拖放并重新种子您的数据库

(在旧分支上)向下倾斜db:reset

  • migrate以撤消迁移。我不确定gem是安装了db/

  • 中的迁移,还是直接从gem运行迁移,所以这并不容易,但是如果您不想擦除数据,这对数据完整性更好。

rake db:schema:load

  • use目前是一个sqlite数据库--虽然不适合生产,但是如果您将数据库签入git,它将表现为您希望当前数据库能够正常运行。不过,我应该警告你,像这样的大文件会很痛苦。最好使用其他方法之一。
票数 3
EN

Stack Overflow用户

发布于 2012-06-14 15:35:39

是的,转换不同迁移级别的分支是一件很混乱的事情。没有“一条线”能正确地做到这一点。有四件事情可能不同步。你需要:

1)确定您的分支机构有哪些迁移:

代码语言:javascript
复制
(look at the contents of db/migrations)

2)确定应用程序认为您的数据库是什么样子:

代码语言:javascript
复制
(look at db/schema.rb)

3)通过查看schema_migrations表确定数据库认为有哪些迁移:

代码语言:javascript
复制
~> rails db console

(opens up your db console, in my case mysql...)

mysql> SELECT * FROM schema_migrations; # don't forget the ';' at the end!

(outputs big huge list of migration timestamps)

4)查看实际的表,看看是否已经应用了迁移。

代码语言:javascript
复制
mysql> describe table_name;

(outputs the column names, see if they have been updated per the migration)

5)现在需要思考的部分。有几种可能的情况。我只能和我遇到的那个人说话:

  • 您的迁移文件(1)、schema.rb(2)和实际表(4)匹配,但是schema_migrations表(3)缺少迁移时间戳。这是我的问题。如果您不关心数据,
    • rake db:reset。它将破坏整个数据库,并从scratch

重新创建结构。

  • 请给出其他解决方案..
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9528242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档