rails-migrations

rails migration: postgresql for md5 of random string as default

。_饼干妹妹 提交于 2019-12-06 02:08:50
Rails 3 + postgresql I want to have a sha of a random string for a default value of a column. So, in my migration I have: t.string :uniqueid, default: md5(random()::text) However i can not get this to actually produce anything, I've used backticks, quotes,etc. From examples that I've seen it seems like that pg function only works in a SELECT statement. Is that accurate? Any ideas on how I could achieve this? Thanks mu is too short Rails will try to interpret this: t.string :uniqueid, default: md5(random()::text) as Ruby code and :default => md5(...) doesn't mean anything in Ruby. If you quote

How to add foreign key in rails migration with different table name

纵然是瞬间 提交于 2019-12-05 13:26:07
问题 How can I assign different table name with adding foreign key. for e.g I have a model like class MyPost < ActiveRecord::Base has_many :comments, class_name: PostComment end class PostComment < ActiveRecord::Base belongs_to :post, class_name: MyPost end Now i want to change my migration file like this: class CreatePostComments < ActiveRecord::Migration def change create_table :post_comments do |t| t.belongs_to :post, index: true t.timestamps null: false end add_foreign_key :post, :class_name =

Reversible migration for change_column_default from not having any default in Rails

主宰稳场 提交于 2019-12-05 10:27:16
问题 The Rails guides to active record migrations says that you can do change_column_default :products, :approved, from: true, to: false I've got a change method in Rails that's similar to the following: change_column_default :people, :height, from: nil, to: 0 with the intention of going from not having any defaults, to having a default of zero. However, when I try rolling it back, I get ActiveRecord::IrreversibleMigration: ActiveRecord::IrreversibleMigration Considering I give Rails a from and to

rake db:migrate doesn't work (Rails 4.0.4)

。_饼干妹妹 提交于 2019-12-05 08:05:50
I have a new app on Rails 4.0.4 / Ruby 2.1.0. The first thing I did was adding Devise gem. When I want to run rake db:migrate, it just does nothing. No error, but the migration isn't executed. Could you please help me what to do with this case? I can't find where is the problem. Thank You! Petr OK, so the problem was that Devise generator generated ".txt" file with migration instead of ".rb" file. Strange, but changing extension solved it. I had the same problem as you Petr and I think I found out why. For some reason when I ran 'rails g devise User', it created a migration (db/migrate/

NoMethodError in Posts#show ;

ぐ巨炮叔叔 提交于 2019-12-05 02:29:38
问题 i have been learning rails through http://guides.rubyonrails.org/getting_started.html. I came across a error while performing save data in controller. The error that comes up when running the blog is :-undefined method `title' for nil:NilClass ** My code for posts_controller.rb is ** class PostsController < ApplicationController def new end def create @post=Post.new(params[:post].permit(:title,:text)) @post.save redirect_to @post end private def post_params params.require(:post).permit(:title

Rails + Postgres migration - why am I receiving the error “PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist”?

眉间皱痕 提交于 2019-12-05 01:53:22
One of my Rails migrations uses a uuid as the primary key. The Postgres extension gen_random_uuid() should solve this issue, but I continue to get the error after installing the relevant extension ( uuid-ossp ). The issue was that the uuid-ossp extension was being blown away with the database each time I dropped the db as part of a reset and migration (e.g. rake db:drop db:create db:migrate ). The fix is to create a migration that's run before all other migrations which enables the relevant extension(s). Like so ( db/migrate/0_enable_extensions.rb ): class EnableExtensions < ActiveRecord:

Should I delete migration after rollback

喜夏-厌秋 提交于 2019-12-05 01:51:20
问题 I'm fairly new to ruby and rails and am just getting my head around migrations. My question is what is the best practice or right time to delete a migration after a rollback. So far what I have read is a matter of opinion whether you delete a migration after a rollback, but is there any major implications to deleting a migration when working in a team, and are there any benefits to leaving the migration file as opposed to deleting it? In my case what would make most sense? I had my original

What determines if rails includes id: :serial in a table definition?

萝らか妹 提交于 2019-12-04 23:43:30
I'm working with an existing rails app, using postgresql. Its schema.rb file has id: :serial for many, but not all, tables: create_table "foos", id: :serial, force: :cascade do |t| When I run rails db:migrate:reset , id: :serial is removed. We are all on the same version of postgres, but different OSes. I haven't exhaustively tested the behavior between machines, but I think there is a difference between machines. The rails version is the same as it was when the project started. The project did start with sqlite3. When I switch to that and regenerate the file, same behavior. What could cause

How to change primary key in rails migration file?

核能气质少年 提交于 2019-12-04 11:50:40
问题 I need to migrate an old mysql table like this: Products name (string, primary_key) to this schema: Products id (integer, primary_key, auto_generated) name (unique) I need the Products.id values populated in the new table. How can i write the rails migration file? I am using Rails 3.2.7 I have 2 problems now: 1. I can't find a method to remove primary key in ActiveRecord::Migration 2. I don't know how to generate values for newly added primary key. 回答1: You could execute arbitrary SQL in your

How to do Rails migration involving Paperclip

让人想犯罪 __ 提交于 2019-12-04 10:29:47
问题 How do people write their Rails migrations that involve Paperclip? I feel that I might be missing something obvious as I have now written my own migration helpers hacks that makes it easier and also take care of doing necessary filesystem changes. And of course you should test run these kinds of migrations in a development (and staging) environment before deploying to production. Paperclip migration rename, add and remove helpers Paperclip change path migration helper (not really a database