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
Related
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.
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]
I have the following app. A Movie has many reviews, a moviegoer has many reviews.
When I try to associate a review with a movie I get the following error
Review Load (0.1ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."movie_id" = 5
SQLite3::SQLException: no such column: reviews.movie_id: SELECT "reviews".* FROM "reviews" WHERE "reviews"."movie_id" = 5
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: reviews.movie_id: SELECT "reviews".* FROM "reviews" WHERE "reviews"."movie_id" = 5
after using a sql gui editor I found that the correct query should be
SELECT "reviews".* FROM "reviews" WHERE "movie_id" = 5
review.rb
class Review < ActiveRecord::Base
belongs_to :movie
belongs_to :moviegoer
attr_protected :moviegoer_id
end
movie.rb and moviegoer.rb have
has_many :reviews
in them.
schema.rb
ActiveRecord::Schema.define(:version => 20130222225620) do
create_table "moviegoers", :force => true do |t|
t.string "name"
t.string "provider"
t.string "uid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "movies", :force => true do |t|
t.string "title"
t.string "rating"
t.text "description"
t.datetime "release_date"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "reviews", :force => true do |t|
t.integer "potatoes"
t.text "comments"
t.integer "moviegoers_id"
t.integer "movies_id"
end
end
What am I doing wrong? why is rails querying "reviews"."movie_id" instead of just "movie_id"?
You have the wrong column name in your migration. The rails convention is that foreign keys are to be singular. If they are not then you need to tell rails what the foreign key is with an options hash on the association.
Either rollback your migration, fix the column name (moviegoers_id is wrong as well) then migrate again, or tell rails the foreign key.
Class Review < ActiveRecord::Base
belongs_to :movie, :foreign_key => 'movies_id'
belongs_to :moviegoer, :foreign_key => 'moviegoers_id'
end
And the same has to happen on the has many side of both models.
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
when i deal with a migrate data,i want to make each record unique in the table the migrate file make.here is my migrate file:
def self.up
create_table :rbac_mandata do |t|
t.integer :mandator_id
t.integer :mandatarius_id
t.integer :permission_id
t.timestamps
end
add_index :rbac_mandata, [:mandator_id, :mandatarius_id, :permission_id], :unique => true
end
when i execute rake task and find it not work.but when i change it to this,it was done:
def self.up
create_table :rbac_mandata do |t|
t.integer :mandator_id
t.integer :mandatarius_id
t.integer :permission_id
t.timestamps
end
add_index :rbac_mandata, [:mandator_id, :permission_id], :unique => true
end
The generated index name is probably too long, it's probably over 64 characters. Just define the name manually and it'll go through fine, like this...
add_index :rbac_mandata, [:mandator_id, :mandatarius_id, :permission_id], :unique => true, :name => 'my_index_name_here'