标签: rails-migrations

在Rails中弹出更新后无法运行迁移

我在运行任何迁移时遇到错误:

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)

ruby-on-rails rails-migrations

13
推荐指数
1
解决办法
4799
查看次数

从架构中删除表 - Rails

我想删除模式中的表.我在第一次启动项目时创建了数据库,并希望删除表.这样做的最佳方式是什么?

我试过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)

ruby ruby-on-rails rails-migrations

13
推荐指数
2
解决办法
2万
查看次数

index:true vs foreign_key:true(Rails)

按照指南,我运行以下命令:

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

13
推荐指数
2
解决办法
9280
查看次数

为什么我需要在Rails中迁移测试数据库?

创建新的迁移文件后,运行迁移,然后运行我的测试,我收到:

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)

ruby-on-rails rails-migrations

13
推荐指数
1
解决办法
4026
查看次数

更新功能分支中的迁移时间戳

假设我的主分支(devlop)和我的功能分支都有活跃的开发.两者都在不断添加迁移.在将功能分支合并到主分支之前,我将把它重新绑定到主分支上.

因此,在最近的开发分支迁移之后,所有功能分支迁移才有意义.

有没有方便/建议的方法来重命名这些文件?我可以生成虚拟迁移并重用为它们生成的时间戳 - 但我想知道是否有一个我不了解的最佳/常见做法?

git ruby-on-rails rails-migrations git-flow

12
推荐指数
1
解决办法
1742
查看次数

ID列的Rails迁移从1,000开始并从那里自动增加?

我希望我的订单模型的ID从1000开始,并从那里自动递增计数.

这可以通过迁移来完成吗?

ruby-on-rails rails-migrations

12
推荐指数
1
解决办法
5202
查看次数

通过单行命令创建模型和索引?

我知道我可以轻松地用这一行创建一个模型.现在假设我想在用户名上添加索引.如何在不进行手动编辑迁移文件的情况下使用一行进行此操作?

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)

ruby-on-rails rails-migrations

12
推荐指数
1
解决办法
8088
查看次数

Rails:创建索引时"t.references"不起作用

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 rails-migrations ruby-on-rails-3.2

12
推荐指数
2
解决办法
1万
查看次数

如何在Ruby on Rails中创建tinyint(2)或tinyint(3)类型的列?

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

mysql ruby-on-rails tinyint rails-migrations

12
推荐指数
3
解决办法
1万
查看次数

奇怪的Rails迁移/ schema.rb问题

前段时间我运行了以下迁移:

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:migratescheme.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)

ruby-on-rails database-schema rails-migrations

11
推荐指数
1
解决办法
552
查看次数