Got a issue when i run rake db:migrate. The mistake message is like
-- create_table(:addresses)
-> 0.1792s
-- contact_id()
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined local variable or method `contact_id' for #<CreateAddresses:0x00000001724718>/var/lib/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/migration.rb:465:in `block in method_missing'
And i used command before migrate
rails g model address street:string city:string region:string postalcode:string country:string contact_id:integer
And my migration file :
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :street
t.string :city
t.string :region
t.string :postalcode
t.string :country
t.integer :contact_id
t.timestamps
end
add_index :addresses, [contact_id, :create_at]
end
end
Could anybody tell where i made mistake please? Thank you a lot.
you are missing : before contact_id:
add_index :addresses, [:contact_id, :create_at]
Related
I've been making a basic schema for a rails app but when I try to migrate it using rails db:migrate, it gives me some weird association errors that I'm not sure what's causing.
== 20211001092658 CreateDevelopers: migrating =================================
-- create_table(:developers)
-> 0.0076s
== 20211001092658 CreateDevelopers: migrated (0.0077s) ========================
== 20211001093122 CreateMessages: migrating ===================================
-- create_table(:messages)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "teams" does not exist
/home/nero/Projects/SMS-Rails/db/migrate/20211001093122_create_messages.rb:3:in `change'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "teams" does not exist
/home/nero/Projects/SMS-Rails/db/migrate/20211001093122_create_messages.rb:3:in `change'
Caused by:
PG::UndefinedTable: ERROR: relation "teams" does not exist
/home/nero/Projects/SMS-Rails/db/migrate/20211001093122_create_messages.rb:3:in `change'
Tasks: TOP => db:migrate
I've been having a few issues setting up Postgres on my machine but I figured most of it would be solved after I created the role, password and database.
Migration files:
#developers
class CreateDevelopers < ActiveRecord::Migration[6.1]
def change
create_table :developers do |t|
t.string :full_name
t.string :email
t.string :mobile
t.timestamps
end
end
end
#teams
class CreateTeams < ActiveRecord::Migration[6.1]
def change
create_table :teams do |t|
t.string :name
t.string :dept_name
t.string :dev_ids, array: true, default: []
t.timestamps
end
end
end
#messages
class CreateMessages < ActiveRecord::Migration[6.1]
def change
create_table :messages do |t|
t.references :team, null: false, foreign_key: true
t.string :title
t.text :content
t.timestamps
end
end
end
I am trying to add user:references onto my already existing model. This is what I originally wrote:
rails g model Post title:string description:text
I do this to add the user:references by running rails generate migration add_user_to_posts user:references, I am receiving this error upon running rake db:migrate:
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists
I am reading the error and I understand I already have a User model, however, I want to add this attribute to the Post model, not the User model.
Db file:
Posts:
class CreatePosts < ActiveRecord::Migration[5.0]
def change
create_table :posts do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
Trying to add the user to posts:
class AddUserToPosts < ActiveRecord::Migration[5.0]
def change
add_reference :posts, :user, foreign_key: true
end
end
Users:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.string :uid
t.string :avatar_url
t.timestamps
end
add_index :users, :uid
end
end
However, rake db:migrate gives me the error above.
I have a rails app that I wrote originally in rail 3, and upgraded to rails 3.1.10 about a month ago. I just created a habtm association between stories and tags, along with the related db migrations.
class Story < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :tags, :uniq => true
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :stories, :uniq => true
end
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
t.timestamps
end
end
end
class CreateStoriesTags < ActiveRecord::Migration
def change
create_table :stories_tags, :id => false do |t|
t.references :story, :tag
end
add_index :stories_tags, [:story_id, :tag_id]
end
end
And finally, here is the relevant schema.db:
create_table "stories", :force => true do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "stories", ["user_id"], :name => "index_stories_on_user_id"
create_table "stories_tags", :id => false, :force => true do |t|
t.integer "story_id"
t.integer "tag_id"
end
add_index "stories_tags", ["story_id", "tag_id"], :name => "index_stories_tags_on_story_id_and_tag_id"
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
I migrated the DB and then played around with the associations in the console. Now I want to rollback the migration so to add some additional columns to the tags table. (I know that I could simply create a new migration, but I would still want to understand why I'm facing this problem.) Now, when I insert rake db:rollback into the console here is the initial output:
[nw0.0.1master (development)]$ rake db:rollback
== CreateStoriesTags: reverting ==============================================
-- drop_table(:stories_tags)
So far so good, but then the rake task just hangs . . . for hours. If I ctrl-c out of it here is the output to the console:
^Crake aborted!
An error has occurred, this and all later migrations canceled:
Interrupt: : ROLLBACK
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `block in execute'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activesupport-3.1.10/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:604:in `execute'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `rollback_db_transaction'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `rescue in transaction'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:182:in `transaction'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/transactions.rb:208:in `transaction'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:742:in `ddl_transaction'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:686:in `block in migrate'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `each'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `migrate'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:553:in `down'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:627:in `move'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:541:in `rollback'
/.rvm/gems/ruby-1.9.2-p290#rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/railties/databases.rake:232:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
Couple of things to note, as this app started out w/ rails 3.0, all early migrations use the older Class.up Class.down methods to define the migrations. Also, when I first expirienced the hanging I switched the CreateStoriesTags migration to use up and down instance methods, but everything still hung.
UPDATE AND THE ANSWER
I noticed that the PGAdmin3 Workbench was also hanging. So, I restarted the Mac and now the rollbacks are working just find.
Turned out to be a problem with my db server. A simple restart fixed the problem
I was having issues migrating databases with mysql and decided to blow the whole thing out of the water and use postgres. I have it installed correctly along with the databases but now I'm getting the same errors I along the lines of when I was using mysql.
$ rake db:migrate
rake aborted!
/Users/beach180/rails_projects/app/db/migrate/20120114221528_create_users.rb:6: syntax error, unexpected ':', expecting keyword_end
t.string "email" :default => "", :null => false
This is the rb file
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.string "first_name", :limit => 25
t.string "last_name", :limit => 50
t.string "email" :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end
def down
drop_table :users
end
end
Got any ideas?
You're missing a comma after the string "email".
t.string "email", :default => "", :null => false
^ comma
creating a simple model with a migration like the following will break the rake db:migrate tasks:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.float :price, :default => "0.00"
t.string :currency, :default => "€"
t.timestamps
end
end
end
Exchanging the Euro sign with EUR fixes the issue temporary but in general I'd love to understand how I could work with € as default value there.
Cheers
Put this on the first line of your migration file:
# encoding: utf-8