I'm doing rail for begineer. I create the model Post with a title as string and body as text area. After that, I forgot to add new element in the form which is sub body
So,I add the sub body in 20150120154140_create_posts.rb and schema.rb as shown below
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
#Add code below
t.string :subbody
t.text :body
t.timestamps
end
end
end
This is my schema.rb
ActiveRecord::Schema.define(version: 20150122040119) do
create_table "posts", force: true do |t|
t.string "title"
t.string "subbody"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
end
After I add it , I write rake db:reset on terminal.
When I checked the db in IRB, Its shown in the table.
After I modified the views for createing new Post and submit it's doesn't save it.
Checked in IRB, its said nil at subbody
rake db:reset doesn't run the migrations. You'll need to rake db:migrate:down followed by rake db:migrate. That should work as long as this is your newest migration. For more info, see this post.
Related
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
I'm trying to add new column into the table by 'rake db:migrate',but it return nothing in cmd.Then i try 'rake db:migrate:status' this time it return the following...
C:\Sites\seas>rake db:migrate:status
database: seas_development
Status Migration ID Migration Name
--------------------------------------------------
up 20160323084854 Create equipment
up 20160329072332 Devise create users
Below is inside my migration file...
class CreateEquipment < ActiveRecord::Migration
def change
create_table :equipment do |t|
t.string :name
t.string :equip_id
t.date :buy_date
t.string :brand
t.string :note
t.date :exp
t.string :status
t.string :serial
t.float :price
t.string :pic_id
t.string :ownby
t.timestamps null: false
end
add_column :equipment, :process ,:string
end
end
This only happen if there exist some data in the table,otherwise migration work fine.
Any suggestion ?
You have a typo
add_column :equipment, :process ,:string
Table name should be in plural
add_column :equipments, :process ,:string
But... if the migration had already be ran, then it will not run again. Create a new migration
rails g migration add_process_to_equipments process
rake db:migrate
Ta dah!
so locally everything works fine. I have 2 simple tables in my DB, i have some migration files and everything. I've been trying to push to heroku for the past hour or so but it's not working. Only 1 table is being migrated, but the second one isn't. I tried creating other migration, but nothing works. Here's my schema:
ActiveRecord::Schema.define(version: 20140804004043) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "folders", force: true do |t|
t.string "name"
t.string "description"
end
create_table "ifolders", force: true do |t|
t.string "name"
t.string "description"
end
create_table "ifoldersx", force: true do |t|
t.string "name"
t.string "description"
end
create_table "images", force: true do |t|
t.string "name"
t.integer "folder_id"
t.string "description"
t.string "url"
end
end
I've tried heroku restart. heroku pg:reset DATABASE. but nothing is working.
Is there a way I can manually add the table into heroku, perhaps with command prompt or something?
Thanks!
EDIT:
Migration Files:
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.string :name
t.integer :folder_id
t.string :description
t.string :url
end
end
end
class CreateIfolders < ActiveRecord::Migration
def change
create_table :ifolders do |t|
t.string :name
t.string :description
end
end
end
class CreateIfoldersx < ActiveRecord::Migration
def change
create_table :ifoldersx do |t|
t.string :name
t.string :description
end
end
end
class CreateFolders < ActiveRecord::Migration
def change
create_table :folders do |t|
t.string :name
t.string :description
end
end
end
Make sure you are also re-migrating the database
heroku pg:reset DATABASE
heroku run rake db:migrate
If that doesn't work add your migration files into your question.
You can easily check if its working by starting up rails console on heroku
heroku run rails c
Edited...
Next step would be to jump in and see what the heroku logs are saying with heroku logs --tail and
$ heroku run rails c
File.open('log/production.log', 'r').each_line { |line| puts line }
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.
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".