rails-migrations

Heroku run rake db:migrate results in no change in the database, app restarted several times

天大地大妈咪最大 提交于 2019-11-27 13:04:38
问题 I have a problem with pushing my migrations to the production database. The issue: I've altered database schema by adding 1 column. I've migrated it to the production database: MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="production" [RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it. == AddLengthColumnToBooks: migrating ========================================= -- add_column(:books, :length, :integer) -> 0.0017s ==

How do I add migration with multiple references to the same model in one table? Ruby/Rails

删除回忆录丶 提交于 2019-11-27 12:51:20
问题 How do I create a migration with two fields that reference the same table? I have tables A, and image. A.image1_id will reference image, and A.image2_id will reference image also. There are only 2 images, not many. If I use class AddFields < ActiveRecord::Migration def change change_table(:ticket) do |t| t.references :image1_id t.references :image2_id end end end I don't think that will work because it will add another _id to the end and probably won't know to use the 'image' model. I also

Add timestamps to an existing table

亡梦爱人 提交于 2019-11-27 09:37:06
问题 I need to add timestamps ( created_at & updated_at ) to an existing table. I tried the following code but it didn't work. class AddTimestampsToUser < ActiveRecord::Migration def change_table add_timestamps(:users) end end 回答1: The timestamp helper is only available in the create_table block. You can add these columns by specifying the column types manually: class AddTimestampsToUser < ActiveRecord::Migration def change_table add_column :users, :created_at, :datetime, null: false add_column

Editing Existing Rails Migrations is a good idea?

人走茶凉 提交于 2019-11-27 07:46:02
问题 When starting out a new project, there are lot of changes in models that I find it easy to edit an existing migration & run db:clean or db:reset than create a new migration. I do this when app has not hit production which means I can reset/clean database without worries & I am working solo or part of a small team. But Today, I came across the following advice in Rails Guide saying its not a good idea & discourages editing existing migrations: Editing existing migrations is not a good idea:

Rolling back a failed Rails migration

感情迁移 提交于 2019-11-27 05:12:15
问题 How do you roll back a failed rails migration? I would expect that rake db:rollback would undo the failed migration, but no, it rolls back the previous migration (the failed migration minus one). And rake db:migrate:down VERSION=myfailedmigration doesn't work either. I've ran into this a few times and it's very frustrating. Here's a simple test I made to duplicate the problem: class SimpleTest < ActiveRecord::Migration def self.up add_column :assets, :test, :integer # the following syntax

Specifying column name in a “references” migration

余生颓废 提交于 2019-11-27 04:09:47
问题 I want to make a migration in Rails, referencing another table. Usually, I would do something like: add_column :post, :user, :references This creates a column named user_id in posts table. But what if, instead of user_id , I want something like author_id ? How can I do that? 回答1: Do it manually: add_column :post, :author_id, :integer but now, when you create the belongs_to statement, you will have to modify it, so now you have to call def post belongs_to :user, :foreign_key => 'author_id' end

Meaning of “Expected string default value for …” on Ruby on Rails

自古美人都是妖i 提交于 2019-11-27 03:46:56
问题 Recently I've created an app for Ruby (2.3.3) on Rails (5.0.0.1): $ rails _5.0.0.1_ new myapp --database=postgresql -T After setting up the Gemfile and testing the connectivity to my databases: $ rails db:migrate I've tried to generate models but I got strange messages: $ rails g model Competition title:string Expected string default value for '--test-framework'; got false (boolean) Expected string default value for '--jbuilder'; got true (boolean) Expected string default value for '--test

Rails 4. Migrate table id to UUID

て烟熏妆下的殇ゞ 提交于 2019-11-27 02:42:14
问题 I have a table: db/migrate/20140731201801_create_voc_brands.rb: class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands do |t| t.string :name t.timestamps end end end But I need to change table to this(if I would create it from zero): class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands, :id => false do |t| t.uuid :id, :primary_key => true t.string :name t.timestamps end add_index :voc_brands, :id end end How can I change this using

Rails rake db:migrate has no effect

不羁的心 提交于 2019-11-27 02:31:19
问题 I made a new Rails 3 app today, added a simple migration, and for some reason, nothing happens when I do rake db:migrate. It simply pauses a few seconds, then returns to the command prompt, with no errors or anything. Schema.rb and the database stay empty. Any ideas what could be going on? I've made many apps and never had this problem. Everything is a totally standard setup too. 回答1: There's a few reasons why your migrations won't run, but the most common is that the system is already under

How do I check the Database type in a Rails Migration?

人走茶凉 提交于 2019-11-27 01:44:40
问题 I have the following migration and I want to be able to check if the current database related to the environment is a mysql database. If it's mysql then I want to execute the SQL that is specific to the database. How do I go about this? class AddUsersFb < ActiveRecord::Migration def self.up add_column :users, :fb_user_id, :integer add_column :users, :email_hash, :string #if mysql #execute("alter table users modify fb_user_id bigint") end def self.down remove_column :users, :fb_user_id remove