Ruby on Rails - sqlite 3 rake migrations not updating the database

↘锁芯ラ 提交于 2020-01-14 05:26:40

问题


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

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

      t.timestamps
    end
  end
end

And I run a migration:

$ bundle exec rake db:migrate

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

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

I run a migration:

$ bundle exec rake db:migrate

And command line gives me the following:

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

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.

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

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

Example of successfully adding a column:

 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) ==============================

Example of unsuccessfully adding a column:

 $ 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.


回答1:


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.

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...




回答2:


I solved the problem undoing the last migration with

rails destroy migration add_index_to_users_email

Then I closed my terminals to ensure that all db connections were closed, then I run

rake db:reset 

Finally, running the same commands again

rails generate migration add_index_to_users_email

Updating the file with the same content and running

rake db:migrate

All the same commands and files that were not working before now worked at all (for me).

I hope this works for you as well.




回答3:


The output you posted from the add index example shows that the migration is indeed running ... but you report it's not actually doing anything. Try bundle exec rake:db rollback (with optional STEP=n to go more than one step back). This really should "just work", and a case of an index might be a little subtle -- try adding a column or something (which you said you have done, but, just to be sure). Does it really not work? How can you tell?



来源:https://stackoverflow.com/questions/9763331/ruby-on-rails-sqlite-3-rake-migrations-not-updating-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!