Have another question on here and am trying not to ask to many but keep hitting walls left and right. Running db:migrate with postgres and getting the below error. How to fix users already exists?
$ rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq1" for serial column "users.id"
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: relation "users" already exists
: CREATE TABLE "users" ("id" serial primary key, "first_name" character varying(25), "last_name" character varying(50), "email" character varying(255) DEFAULT '' NOT NULL, "password" character varying(40), "created_at" timestamp, "updated_at" timestamp)
Tasks: TOP => db:migrate
rb file below
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
You can place a command before the create_table :users do |t| to drop the table do drop_table :users. ONLY DO THIS IF YOU HAVE NO DATA TO LOSE!
If you want to added columns put add_column for each line eg. add_column :users, :name, :string
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'm trying to rename a column that is definitely there in the schema and all the info is there, but when I go to update it it's giving me a "missing column" error and I can't figure out why? Any ideas?
Migration:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :postcodes, :type, :zip_type
end
end
schema.rb
create_table "postcodes", force: true do |t|
t.string "postalcode"
t.string "type"
t.string "primary_city"
t.string "state"
t.string "county"
t.string "timezone"
t.string "area_code"
t.string "latitude"
t.string "longitude"
t.string "estimated_population"
t.datetime "created_at"
t.datetime "updated_at"
end
Error:
DL is deprecated, please use Fiddle
DL is deprecated, please use Fiddle
== 20150304172437 ChangeColumnName: migrating =================================
-- rename_column(:postcodes, :type, :zip_type)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Missing column postcodes.typeC:/Users/Steve Q/Documents/GitHub/project1/db/migrate/20150304172437_change_column_name.rb:3:in `change'C:in `migrate' ActiveRecord::ActiveRecordError: Missing column postcodes.type
I have tried renaming type column to zip_type its working. Another solution is you can delete type column and add new column as zip_type.
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