rails-migrations

Editing Existing Rails Migrations is a good idea?

大憨熊 提交于 2019-11-28 13:35:28
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: you will be creating extra work for yourself and your co-workers and cause major headaches if the

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

爱⌒轻易说出口 提交于 2019-11-28 10:44:40
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-framework'; got false (boolean) invoke active_record create db/migrate/20161206021603_create_competitions

Rails 4. Migrate table id to UUID

女生的网名这么多〃 提交于 2019-11-28 09:04:44
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 migration? I had the same problem as yours. To migrate from default id to use uuid, I think you could

Rails rake db:migrate has no effect

瘦欲@ 提交于 2019-11-28 08:55:29
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. There's a few reasons why your migrations won't run, but the most common is that the system is already under the impression that all the migrations you've defined have already run. Each migration creates an entry in

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

北城以北 提交于 2019-11-28 06:43:38
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_column :users, :email_hash end end EmFi ActiveRecord::Base.connection will provide you with everything

Generate an auto increment field in rails

和自甴很熟 提交于 2019-11-28 05:30:20
I have a model Token, which has a field token_number that I need to auto increment (starting from 1001), if and only if the user does not provide it. The problem is that, since the user has the option to provide this field, I can't exactly query the database and ask for the largest token_number. I found one answer on this forum, but I'm quite certain there has to be a better way to do it than to execute an SQL statement? Auto increment a non-primary key field in Ruby on Rails Syed Aslam Interesting question for me. Unfortunately, rails doesn't provide a way to auto-increment columns, so we

Rolling back a failed Rails migration

隐身守侯 提交于 2019-11-28 03:44:44
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 error will cause the migration to fail add_column :asset, :test2, :integer end def self.down remove_column

What is the best way to drop a table & remove a model in Rails 3?

江枫思渺然 提交于 2019-11-27 19:54:17
问题 I have a model & a table which I no longer need in my App, I could leave them there but I would like to remove them to keep things tidy. I'm trying to figure out the best way to remove them with out messing around with my migrations & db/schema.rb files & any side effect it could have on my production environment, my app is on Heroku. I'm using PostgreSQL on both my local machine & heroku. So far I've found two ways to do this but am not sure which is the best method/ rails way? Method 1 I

Specifying column name in a “references” migration

吃可爱长大的小学妹 提交于 2019-11-27 16:54:18
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? mschultz 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 In Rails 4.2+ you can also set foreign keys in the db as well, which is a great idea . For simple

Rails Migrations: tried to change the type of column from string to integer

若如初见. 提交于 2019-11-27 13:29:25
I created a table in my rails app with rails generate migrations command. Here is that migration file: class CreateListings < ActiveRecord::Migration def change create_table :listings do |t| t.string :name t.string :telephone t.string :latitude t.string :longitude t.timestamps end end end Then I wanted to store the latitude and longitude as integers so I tried to run: rails generate migration changeColumnType and the contents of that file are: class ChangeColumnType < ActiveRecord::Migration def up #change latitude columntype from string to integertype change_column :listings, :latitude,