Ruby on Rails-sqlite 3 rake迁移未更新数据库 [英] Ruby on Rails - sqlite 3 rake migrations not updating the database

查看:72
本文介绍了Ruby on Rails-sqlite 3 rake迁移未更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RoR(3.2.2)和SQLite 3(1.3.5)。最初生成模型时,我能够成功创建数据库。但是,每当我尝试使用迁移生成器时,命令行中似乎都没有任何问题(没有错误),但是当我检查数据库时,没有任何更新或更改。

I am using RoR (3.2.2) and SQLite 3 (1.3.5). When I initially generate a model I am able to successfully create a database. However, whenever I try to use the migration generator it appears to not have any issues in the command line (no errors), but when I check the database nothing has updated or changed.

例如,我创建此数据库:

For example, I create this database:

$ rails generate model User name:string email:string

db / migrate / [timestamp] _create_users.rb

db/migrate/[timestamp]_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

然后运行迁移:

$ bundle exec rake db:migrate

到目前为止,到目前为止,我使用SQLite检查数据库数据库浏览器,一切看起来都应该正确。

So far so good, I check my database using SQLite Database Browser and everything looks as it should.

然后,如果我想添加索引:

Then if I want to add an index:

$ rails generate migration add_index_to_users_email

db / migrate / [timestamp] _add_index_to_users_email。 rb

db/migrate/[timestamp]_add_index_to_users_email.rb

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

我运行迁移:

$ bundle exec rake db:migrate

命令行提供了以下内容:

And command line gives me the following:


捆绑执行耙db:migrate
== AddIndexToUsersEmail:迁移========================= ===============
== AddIndexToUsersEmail:迁移(0.0000s)==================== =============

bundle exec rake db:migrate == AddIndexToUsersEmail: migrating =========================================== == AddIndexToUsersEmail: migrated (0.0000s) ==================================

但是,当我使用SQLite数据库浏览器检查数据库时,它不是 t更新。如果尝试将新列添加到表等中,则会得到相同的结果。我能够进行迁移的唯一方法是使用SQLite数据库浏览器手动更新数据库。对于为什么它不能通过Rails和命令行运行的任何帮助,将不胜感激。

However, when I check my database using SQLite Database Browser it isn't updated. I get the same results if I try to add new columns to the table, etc. The only way I have been able to do migrations is manually updating the database using the SQLite Database Browser. Any help as to why it is not working through Rails and the command line would be greatly appreciated.

这是我的gemfile:

Here is my gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.2'
gem 'bootstrap-sass', '2.0.0'

group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.8.1'
gem 'annotate', '~> 2.4.1.beta'
end

group :assets do
gem 'sass-rails',   '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.0'

group :test do
gem 'capybara', '1.1.2'
end

group :production do
gem 'pg', '0.12.2'
end

Database.yml

Database.yml

development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000

成功添加列的示例:

 rails generate migration add_password_digest_to_users password_digest:string
  invoke  active_record
  create    db/migrate/20120318235656_add_password_digest_to_users.rb
 $ subl db/migrate/[timestamp]_add_password_digest_to_users.rb
 $ bundle exec rake db:migrate
 ==  AddPasswordDigestToUsers: migrating =======================================
 -- add_column(:users, :password_digest, :string)   -> 0.0008s
 ==  AddPasswordDigestToUsers: migrated (0.0009s) ==============================

添加列失败的示例:

 $ rails generate migration add_remember_token_to_users
  invoke  active_record
  create    db/migrate/20120319010623_add_remember_token_to_users.rb
 $ subl db/migrate/[timestamp]_add_remember_token_to_users.rb
 $ bundle exec rake db:migrate
 ==  AddRememberTokenToUsers: migrating ========================================
 ==  AddRememberTokenToUsers: migrated (0.0000s) ===============================

当更新数据库失败时,迁移时间为零。我不确定自己在做什么错。预先感谢您的任何建议。

Notice when it fails to update the database the migration time is zero. I'm not sure what I am doing wrong. Thanks in advance for any suggestions.

推荐答案

我认为发生的事情是您将语法稍有错误(这很棘手),然后您进行无任何操作的空迁移。

I think what happens is you get the syntax slightly wrong (it's insanely tricky) then you get an empty migration which doesn't do anything.

我会仔细检查实际的迁移文件是否具有添加列。我经常最终会手动输入实际的更改(或向上/向下)。

I would double check that the actual migration file has add column. I often end up manually putting the actual change (or up/down) in.

正如哈里森所说,添加索引更加微妙-您确定不会。如果您刷新sqlite浏览器,则不会添加。您给出的有效示例...是添加列...

As t Harrison says, adding an index is more subtle - as you sure it doesn't get added if you refresh the sqlite borwser. The examples you gave that worked... are to add columns...

这篇关于Ruby on Rails-sqlite 3 rake迁移未更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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