我在运行任何迁移时遇到错误:
raj@notebook-pc:~/Desktop/Projects/invoicemanagement$ rails g migration RemoveDescriptionOfGoodsFromInvoiceDetails description_of_goods:string
Warning: You're using Rubygems 1.8.23 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
/var/lib/gems/1.9.1/gems/bundler-1.9.0/lib/bundler/runtime.rb:34:in `block in setup': You have already activated spring 1.3.3, but your Gemfile requires spring 1.3.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
from /var/lib/gems/1.9.1/gems/bundler-1.9.0/lib/bundler/runtime.rb:19:in `setup'
** 11 stack trace lines skipped **
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
Run Code Online (Sandbox Code Playgroud)
所以通过一些谷歌搜索我运行捆绑更新弹簧,它解决了上述错误,我能够删除/添加迁移.再次,如果我运行rake db:migrate,我收到错误:
rake aborted!
StandardError: An error has …Run Code Online (Sandbox Code Playgroud) 我想删除模式中的表.我在第一次启动项目时创建了数据库,并希望删除表.这样做的最佳方式是什么?
我试过rails g migration drop table :installs但是只是创建了一个空迁移?
架构:
create_table "installs", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", 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, null: false
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", null: false
t.datetime "updated_at", null: false
end
add_index "installs", ["email"], name: "index_installs_on_email", unique: true
add_index "installs", ["reset_password_token"], name: "index_installs_on_reset_password_token", unique: true
Run Code Online (Sandbox Code Playgroud) 按照指南,我运行以下命令:
rails g migration CreateSnippetsUsers snippet:belongs_to user:belongs_to
Run Code Online (Sandbox Code Playgroud)
这创建了以下迁移:
class CreateSnippetsUsers < ActiveRecord::Migration[5.0]
def change
create_table :snippets_users do |t|
t.belongs_to :snippet, foreign_key: true
t.belongs_to :user, foreign_key: true
end
end
end
Run Code Online (Sandbox Code Playgroud)
在过去,我见过同样的事情,而index: true不是foreign_key: true.这两者有什么区别?
ruby-on-rails rails-migrations rails-activerecord ruby-on-rails-5
创建新的迁移文件后,运行迁移,然后运行我的测试,我收到:
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::PendingMigrationError:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
是不是rails_helper.rb应该将迁移应用到我的测试数据库中的以下片段?
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
Run Code Online (Sandbox Code Playgroud)
更新
这是我的config/environments/test.rb要求:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
# test suite. You never need to work with …Run Code Online (Sandbox Code Playgroud) 假设我的主分支(devlop)和我的功能分支都有活跃的开发.两者都在不断添加迁移.在将功能分支合并到主分支之前,我将把它重新绑定到主分支上.
因此,在最近的开发分支迁移之后,所有功能分支迁移才有意义.
有没有方便/建议的方法来重命名这些文件?我可以生成虚拟迁移并重用为它们生成的时间戳 - 但我想知道是否有一个我不了解的最佳/常见做法?
我希望我的订单模型的ID从1000开始,并从那里自动递增计数.
这可以通过迁移来完成吗?
我知道我可以轻松地用这一行创建一个模型.现在假设我想在用户名上添加索引.如何在不进行手动编辑迁移文件的情况下使用一行进行此操作?
script/rails generate model TwitterUser username:string num_followers:integer num_following:integer bio:string location:string image:string num_tweets:integer website:string
Run Code Online (Sandbox Code Playgroud) class CreateBallots < ActiveRecord::Migration
def change
create_table :ballots do |t|
t.references :user
t.references :score
t.references :election
t.string :key
t.timestamps
end
add_index :ballots, :user
add_index :ballots, :score
add_index :ballots, :election
end
end
Run Code Online (Sandbox Code Playgroud)
结果是:
SQLite3::SQLException: table ballots has no column named user: CREATE INDEX "index_ballots_on_user" ON "ballots" ("user")/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change'
Run Code Online (Sandbox Code Playgroud)
我以为t.references应该为我处理这件事?
在Ruby on Rails中,迁移中的以下代码tinyint(4)在MySQL中创建了一个类型列:
create_table :great_table do |t|
t.integer :step_position, :limit => 1 #tinyint
end
Run Code Online (Sandbox Code Playgroud)
我将如何创建类型的列tinyint(2)或tinyint(3)?
前段时间我运行了以下迁移:
class CreatePipelineSpecs < ActiveRecord::Migration
def change
create_table :pipeline_specs do |t|
t.integer :id_no
t.string :od
t.string :wt
t.string :material
t.string :spec_type
t.string :spec_grade
t.string :mop
t.string :stress_level
t.string :joints
t.text :notes
t.string :ip
t.references :pipeline, index: true, foreign_key: false
t.timestamps null: false
end
add_index :pipeline_specs, :id_no
end
end
Run Code Online (Sandbox Code Playgroud)
我不知道现在发生了什么事,但我每次运行时rake db:migrate的scheme.rb文件被使用更新:
create_table "pipeline_specs", force: :cascade do |t|
t.integer "id_no"
t.string "od"
t.string "wt"
t.string "material"
t.string "spec_type"
t.string "spec_grade"
t.string "mop"
t.string "stress_level"
t.string "joints"
t.text "notes" …Run Code Online (Sandbox Code Playgroud)