Migration changes show in Schema file but not in the DB - ruby-on-rails

For Rails 3.2 I have written this migration to rename the column name as seen in the migration
class RenameKpiColumn < ActiveRecord::Migration
def change
rename_column(:key_performance_intervals, :kpi_id, :key_performance_interval_id)
end
end
And then I said bundle exec rails db:migrate
If I go to Schema.rb I see this for that table, so looks likes it picked the new column name from Migration:
create_table "key_performance_intervals", :force => true do |t|
t.integer "key_performance_interval_id"
t.integer "interval"
t.integer "interval_unit"
t.decimal "count"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
But if I open the pgAdmin tool and look at the tables and column names in there, it is still using the old column name of kip_id .
Is there any step I am missing?

Since migrating the database gives no output, it seems that the migrations ran fine. Just restart pAdmin and the changes should be reflected there.
To also prepare your test database, run
$ rake db:test:prepare

Related

rake db:migrate does not create table

I'm using Rails 4.2.6 and have strange error (learning rails with the HeadFirst book, project and directory name - "mebay"):
I need to create project uses only "read" of CRUD - so I run:
~/mebay $ rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string
Running via Spring preloader in process 8400
invoke active_record
identical db/migrate/20161018144410_create_ads.rb
identical app/models/ad.rb
invoke test_unit
identical test/models/ad_test.rb
identical test/fixtures/ads.yml
and here comes my db/migrate/20161018144410_create_ads.rb:
class CreateAds < ActiveRecord::Migration
def change
create_table :ads do |t|
t.string :name
t.text :description
t.decimal :price
t.integer :seller_id
t.string :email
t.string :img_url
t.timestamps null: false
end
end
end
(looks pretty ok for me, basing on earlier projects)
Then, as I understand, I need to create database (i use sqlite):
~/mebay $ rake db:migrate
but after that, my development.sqlite3 remain empty
what am i doing wrong?
You actually didn't do anything wrong. Look in db/schema.rb and if you see this then you're database is setup properly:
ActiveRecord::Schema.define(version: 20161019035406) do
create_table "ads", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price"
t.integer "seller_id"
t.string "email"
t.string "img_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
If you're getting an error that the table already exists, you can do the following:
first add a down method to your migration file that you used to generate the ads table. The down method would look like this:
def down
drop_table :ads
end
Then run rake db:migrate:down VERSION=version_number where the version_number is the timestamp in the name of the migration file. This will remove the table from the database. Then change the name of the change method to up. Save the file and run rake db:migrate:up to create the table again.
How about run rake db:create first and then run rake db:migrate

Why are all of my tables in Rails 4/Postgres being created with "dimension" of 1?

I'm running into a LOT of problems just trying to do basic model generations and migrations in Rails 4 with Postgres. I have the pg gem installed, version 0.17.1.
In the beginning, I couldn't even run migrations without errors, because the schema_migrations table was created with the version column having a dimension of 1. Once I manually changed it to zero, it worked fine.
Now, if I look at all of the migrations that resulted from me using the Rails model generator, I see that every single column, with the exception of the id column in each table, was created with dimension of 1.
Is there some default setting I need to change? Is this somehow correct and I am messing up something else? This is my first Rails 4 project, so I'm just trying to figure out why it would want all of those columns to default to an Array.
Here is one of my migration files:
class CreateCatalogs < ActiveRecord::Migration
def change
create_table :catalogs do |t|
t.string :name
t.text :description
t.string :schema_name
t.string :catalog_type
t.boolean :billable
t.timestamps
end
end
end
And this is from schema.rb:
create_table "catalogs", force: true do |t|
t.string "name", array: true
t.text "description", array: true
t.string "schema_name", array: true
t.string "catalog_type", array: true
t.boolean "billable", array: true
t.datetime "created_at", array: true
t.datetime "updated_at", array: true
end
What in the heck!
As luck would have it, Ruby on Rails v4.0.3 was released today. I did the following:
Upgrade Rails
deleted db/migrate/schema.rb
Delete all 3 databases (dev, test, production)
ran rake db:setup
ran rake db:migrate
Looked at the new db/migrate/schema.rb to make sure it was OK
ran rake db:test:prepare
Sure enough, the problem is fixed in this new release. I couldn't find a record of the problem anywhere! It's been an issue for a few weeks. Anyway, fixed!
It may be your migration that specifies each column to be array

Postgres error when I try to run heroku run rake db:migrate

My sqlite3 database works fine in development but when I try to migrate it to production I get the following error:
PG::Error: ERROR: relation "movies" does not exist
: ALTER TABLE "movies" ADD COLUMN "production_company" character varying(255)/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `async_exec'
I know a few people have posted about this but nothing I've tried seems to work. Anyone know how I might fix this?
Here's the migration:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
add_column :movies, :production_company, :string, :limit => nil
end
end
Here's my schema.rb file if this helps:
ActiveRecord::Schema.define(:version => 20130331014529) do
create_table "movies", :force => true do |t|
t.string "title"
t.string "actor_1"
t.string "locations"
t.string "release_year"
t.string "string"
t.string "actor_2"
t.string "actor_3"
t.string "writer"
t.string "director"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "production_company"
t.string "distributor"
t.string "fun_facts"
end
end
Here's the migration where I create the movies table:
class Movies < ActiveRecord::Migration
def up
end
def down
end
end
It's not the best approach but a quick fix would be to replace that migration with this:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :production_company
t.timestamps
end
end
end
Your migration where you create the movie table is incorrect. The up and down methods you have don't do anything. Because of this, there is no movie table to add the production_company column to.
You need something like this;
class Movies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :title
t.string :actor
.
. #add your columns you want in your initial migration here
.
end
end
I can't say why things worked in development in SQLite but at some point you successfully created the movies table and then maybe you altered the migration after that. This is easy to do (I've done it!).
A lot of people recommend that when you set up production you don't run your migrations to set up the database, but instead use rake db:schema:load (in fact, if you read the comments at the top of your db/schema.rb file it specifically describes this).
Another point is that a lot of people recommend having the same database in development as in production as there are subtle differences that can lead to unexpected problems in production (this is not what has caused your issue). If you're only just starting out then don't worry about it for now; it's just one more headache to set up PostgreSQL on your own machine; but it's something to keep in mind as you progress.

Ruby on Rails - Creating Wrong Schema.rb File?

Am I correct in saying that the db/schema.rb file should be pulling from the db/migrate files on rake db:migrate? I am running a rake db:migrate and it is adding a table that isn't defined in the migrate, nor the models. Any ideas?
Migrate Files (just one):
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :hashed_password
t.timestamps
end
end
end
Resulting Schema after rake:
ActiveRecord::Schema.define(:version => 20121113214159) do
create_table "user_categories", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email"
t.string "hashed_password"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
I had added a user_categories scaffolding earlier, but incorrectly so I destroyed it. Not sure where I went wrong in destroying parts...
If you don't have any important data on your db, you could run rake db:drop then rake db:create. Then run rake db:migrate and it should update your schema clean.
It's important to note that rake db:migrate will poll your database for its current state (separate of migrations) and update schema.rb accordingly. If you go into SQL command line and add a table, for example, and then run rake db:migrate, schema.rb will reflect that new table whether there's a migration or not.
Could be that your forgot to define environment variables.
Open Gemfile and verify that you don't use the sqlite gem for your environment
Type the rake db:drop then rake db:drop commands if as output you receive empty string probably you forgot to setup environment variables
Checkout the config/database.yml file and find places where you could use them. You should find something like that: database: <%= ENV["<DB_NAME"] %>
Ensure that your variables has been set before. If you use the dotenv or figaro gems, probably you forgot to create appropriate file or define variables there

remove_column not removing column or giving any errors in rails

I have a sqlite3 db in a rails app with the following schema
ActiveRecord::Schema.define(:version => 20100816231714) do
create_table "comments", :force => true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", :force => true do |t|
t.string "name"
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tags", :force => true do |t|
t.string "name"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I began with a Post :has_many relationship with tags so each tag has a post_id reference.
I now want to change this relationship to a 'has_and_belongs_to_many', I know i have to create the joins table etc.... this isn't a problem and is working
The problem comes in when i try remove the post_id form the tags table. My migration looks like this:
class RemoveFieldsToTags < ActiveRecord::Migration
def self.up
remove_column :tags, :post_id
end
def self.down
add_column :tags, :post_id, :references
end
end
When I run rake db:migrate and rake db:migrate:up VERSION= Nothing happens when I run rake db:migrate:down VERSION= I get column:
SQLite3::SQLException: duplicate column name: post_id: ALTER TABLE "tags" ADD "post_id" references
Any one know whats going on?
It sounds as if Rails thinks your DB is up to date (given that nothing happens when you run db:migrate). You can get into this state if you've modified your migration after applying it (common during development).
Have you tried running db:migrate on a fresh db (note this will wipe your database)?
rake db:drop db:create db:migrate
Like avaynshtok mentions above, it sounds like rails thinks your migrations are up to date (as in, they have all been applied), but to you they are not (the post_id column is still on the tags table).
A common 'workaround' to deal with this situation without having to wipe your database is commenting out the 'down' method of your migration and running
rake db:migrate:redo
Given the 'down' is commented out, it won't try to add the column again, so it will proceed to reapply the 'up' method, removing your 'post_id' column. You can then remove the comment on the 'down' method and it should all be good.
PS. You might want to look into using a 'has_many :through' type of relationship instead of 'has_and_belongs_to_many' as well.
I had a similar problem to the op, but had to manually delete the databases and then run
rake db:create db:migrate
rake db:migrate:redo
and
rake db:drop
did not work for me as it kept saying "db/test.sqlite3 already exists".

Resources