rails-migrations

Rails creating schema_migrations - Mysql2::Error: Specified key was too long

不问归期 提交于 2019-12-04 05:06:40
I am using Rails 3.2.6 and Mysql 6.0.9 (but I have exactly the same error on MySQL 5.2.25) When I create new database ( rake db:create ) and then when I try to load the schema ( rake schema:load ) I get this error: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`) After hours and hours of research I found these solutions: 1. Change MySQL variable innodb_large_prefix to true (or ON) This didn't work. I tried it on my Linux server, my Mac and even on Windows - it just doesn't work. 2.

Rails4 create join tabel no need to add primary key in Migration?

邮差的信 提交于 2019-12-04 04:39:57
问题 I use this command : rails g migration CreateJoinTableUserPloy user ploy And i check the Migration file: create_join_table :Users, :Posts do |t| #t.index [:user_id, :ploy_id] #t.index [:ploy_id, :user_id] end There are 2 index is be commented by defualt. Then i run this command: rake db:migrate Then i check my database structure And i not seen primary key, Does it mean that join tabel no need add index and primary key in database structure? 回答1: Consistent with http://meta.serverfault.com/a

add a database column with Rails migration and populate it based on another column

这一生的挚爱 提交于 2019-12-04 03:16:36
I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(:type, c) end end def self.down end end It's generally a bad idea to reference your models from your

Rails 3.2.6 & database views creation through migrations

China☆狼群 提交于 2019-12-04 01:35:00
I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method. Unfortunately the migration generates a table, not a view. Why? Many thanks in advance, Mauro UPDATE: I would like to have something as follows: class CreateMyView < ActiveRecord::Migration def self.up execute <<-SQL CREATE VIEW my_view AS SELECT ... SQL end def self.down execute <<-SQL DROP VIEW my_view SQL end end Unfortunately this migration creates a table... UPDATE: the previous code works! I was executing rake db:reset instead of rake db

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

£可爱£侵袭症+ 提交于 2019-12-04 00:23:01
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 => MyPost end end But it is not working. Migration is getting cancelled. How do I change my migration

Reversible migration for change_column_default from not having any default in Rails

拟墨画扇 提交于 2019-12-04 00:20:54
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 , why isn't it accepting it? I'm using Rails 4.2.0. if you are using mysql as adapter, then according

How to add sequences to a migration and use them in a model?

你离开我真会死。 提交于 2019-12-03 19:47:26
问题 I want to have a " Customer " Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use PostgreSQL. Have a look at my migration: class CreateAccountsCustomers < ActiveRecord::Migration def up say "Creating sequenze for customer number starting at 1002" execute 'CREATE SEQUENCE customer_no_seq START 1002;' create_table :accounts_customers

Couldn't run migration after spring update in Rails

人盡茶涼 提交于 2019-12-03 16:34:11
问题 I am facing a error when I run any migration as: 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

How do you make remove_column reversible?

风流意气都作罢 提交于 2019-12-03 15:31:31
问题 I have a migration that removes a column: def change remove_column :foos, :bar, :boolean end When I try to rake db:rollback that migration, I get the following error: remove_column is only reversible if given a type. The ActiveRecord::Migration documentation says that the following is the signature for remove_column : remove_column(table_name, column_name, type, options) So my type in this case should be :boolean , and I expect that migration to be reversible. What am I missing? I can

Rails ActiveRecord::Migration what is the difference between index: true and add_index?

一世执手 提交于 2019-12-03 15:04:21
问题 What is the difference between t.boolean :is_live, index: true and add_index :table_name, :is_live If there is no difference, how come only the add_index is reflected in schema.rb. When I use index: true , I can't actually see the index in schema.rb . Should I only use the add_index method. When use the add_index method, I can see this in my schema.rb add_index "table_name", ["is_live"], name: "index_table_name_on_is_live", using: :blahblah 回答1: In short: both do the same job. ìndex: true`