rails-migrations

Rails 3 migrations: boolean (mysql vs postgreSQL)

不羁岁月 提交于 2019-12-10 14:31:57
问题 I'm trying to add a "sticky" option on my forum topics. This is how my migration looks like def self.up add_column :topics, :sticky, :boolean, :null => false, :default => false end def self.down remove_column :topics, :sticky end This works perfect locally on mysql, but when I push the changes to heroku (which uses PostgreSQL), this is what I get when using the console >> t.sticky => "f" >> t.sticky.class => String >> t.sticky = true => true >> t.sticky.class => TrueClass Why is the default

Change foreign key column name in rails

浪子不回头ぞ 提交于 2019-12-10 12:44:15
问题 I have a Project migration class like this: class CreateProjects < ActiveRecord::Migration def change create_table :projects do |t| t.string :title t.text :description t.boolean :public t.references :user, index: true, foreign_key: true t.timestamps null: false end end end It creates a column name user_id in projects table but I want to name the column owner_id so I can use project.owner instead of project.user . 回答1: You can do it two ways: #app/models/project.rb class Project < ActiveRecord

rails migration: postgresql for md5 of random string as default

一世执手 提交于 2019-12-10 10:22:08
问题 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 回答1: Rails will try to interpret this: t.string :uniqueid, default: md5(random

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

烈酒焚心 提交于 2019-12-09 17:21:48
问题 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

Rails migration change sequence or order

丶灬走出姿态 提交于 2019-12-08 14:43:57
问题 I wrote a few migrations for my Rails 3 app, but I would like to change the order of the migrations. How can I change the migration order or sequence? Is it as simple as renaming the migration file with what appears to be the timestamp? I know this is an odd question, but basically, I made a mess of my migrations and removed some old migrations and now I need to drop a table before creating a new one. I also know I can include the drop statement in the create-the-new-table migration, but I'm

What is a “circular argument reference” error, with activesupport time_zone?

给你一囗甜甜゛ 提交于 2019-12-08 14:39:57
问题 I'm new in ruby on rails, and I am trying to create a tutorial. I have a problem when I execute rake db:migrate . hugo@ubuntu:~/pin_board$ rake db:migrate /home/hugo/.rvm/gems/ruby-2.2.2/gems/activesupport-4.0.5/lib/active_support/values/time_zone.rb:283: warning: circular argument reference - now What causes this? Can somebody help me? 回答1: You see this warning message because your Rails version is 4.0.5. The issue has been fixed on Rails 4.0.6. Update your Gemfile and run bundle update

Rails 4 MySQL bigInt primary key issues and errors

旧街凉风 提交于 2019-12-07 20:10:50
问题 I need to use a 14-digit bigInt as a primary key in a rails 4.1.8 application. Using older posts on SO as a guide, I came up with the following to address this... class CreateAcctTransactions < ActiveRecord::Migration def change create_table "acct_transactions", :id => false do |t| t.integer :id, :limit => 8,null: false t.integer "account_id",limit: 8,null: false t.integer "transaction_type_id", null: false t.datetime "date",null: false t.text "description",limit: 255 t.decimal "amount"

Rails Engines with problems in Foreign keys

亡梦爱人 提交于 2019-12-07 12:38:40
问题 I'm developing a rails engine and this is my gem.gemspec s.required_ruby_version = '>= 2.0.0' s.add_dependency 'rails', '>= 4.2.0' s.add_dependency 'enumerate_it' s.add_dependency 'slim-rails' s.add_dependency 'bootstrap-sass' s.add_dependency 'jquery-rails' s.add_development_dependency 'rdoc' s.add_development_dependency 'tomdoc' s.add_development_dependency 'sqlite3' s.add_development_dependency 'rspec-rails' s.add_development_dependency 'timecop' s.add_development_dependency 'shoulda

Rails and MySQL syntax error with multiple SQL statements in an execute block

点点圈 提交于 2019-12-07 08:35:49
问题 I have the following code in a Rails migration for an app that uses MySQL: execute <<-SQL ALTER TABLE properties ADD name VARCHAR(255) NOT NULL; ALTER TABLE properties ADD CONSTRAINT fk_properties_name FOREIGN KEY (name) REFERENCES valid_property_names (property_name); SQL When I run the migration, I get the following error: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE

Rails 3 - DB seed data validation

人盡茶涼 提交于 2019-12-06 09:32:37
I am seeding a test database in Rails 3.1 through thousands of create calls in the seeds.rb file. A little problem arises when these calls do not pass the model validations: rails will not notify me this, and the seeding goes on correctly until the end of the file. At the end of the process I do not know which records have been created and which aren't, unless I check them one by one ... Is there a way to get notified when records do not pass validations when using rake db:seed or rake db:reset ? Thank you! You can create validations you want in the Models and use ModelName.create! . This will