1

I'm following this tutorial, and I'm having a problem when running rake db:migrate

In db/migrate I have the create_post.rb file:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :text
      t.timestamps
    end
  end
end

But it does not create the table. My database.yml file is:

development:
 adapter: mysql2
 encoding: utf8
 database: blog_development
 pool: 5
 username: root
 password:
 socket: /tmp/mysql.sock

The output from rake db:migrate seems ok.

I'm using phpMyAdmin to handle the database, which is correctly created manually by me.

What am I doing wrong?

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
rdiazroman
  • 419
  • 1
  • 8
  • 18
  • Do you check if your table is created in phpMyAdmin or with the rails console ? – Lukas Warsitz Mar 27 '14 at 11:41
  • have you created this migration file from terminal or manually if manually make sure time stamp is larger than last migration time stamp.. – neo-code Mar 27 '14 at 11:42
  • no errors from output: – rdiazroman Mar 27 '14 at 11:42
  • C:\Sites\blog>rake db:migrate:redo VERSION=20140325180152 == 20140325180152 CreatePosts: reverting ====================================== -- drop_table(:posts) -> 0.0010s == 20140325180152 CreatePosts: reverted (0.0250s) ============================= == 20140325180152 CreatePosts: migrating ====================================== -- create_table(:posts) -> 0.0390s == 20140325180152 CreatePosts: migrated (0.0400s) ============================= – rdiazroman Mar 27 '14 at 11:42
  • if no out put this means rake is not finding any migration to run..check your schema file if posts is there ? – neo-code Mar 27 '14 at 11:43
  • are you looking at the correct database (`blog_development`)? – Uri Agassi Mar 27 '14 at 11:46
  • yes, I look into the correct database. mysql> select * from posts; ERROR 1146 (42S02): Table 'blog_development.posts' doesn't exist – rdiazroman Mar 27 '14 at 11:48
  • could be a problem with the adapter: mysql2 ?? – rdiazroman Mar 27 '14 at 12:05
  • drop the database and run the migration again. Once a migration is executed, its timestamp is added to the schema so that when the next time you run db:migrate, the migrations that have already been executed dont execute again. – Prasad Surase Mar 27 '14 at 12:18
  • Also, are you sure phpmyadmin is looking at the correct place? Try this from the command line: mysql -uroot blog_development -e "show tables;" – Allyl Isocyanate Mar 27 '14 at 13:31
  • I experienced the same problem - I deleted the migration file, ran rake db:migrate - added migration again file and then ran again rake db:migrate – epsilones Dec 17 '14 at 12:12

3 Answers3

2

If you are connecting to the right database everything seems fine to me.. I had a similar problem a few weeks ago and the accepted answer of this question fixed my issue.

Here are the steps to run:

rake db:drop:all
rake db:create:all
rake db:migrate

I hope it will fix your problem.

WARNING: this will erase your database.

Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20
  • no errors in output from these commands. But the table is still not created :/ – rdiazroman Mar 27 '14 at 11:59
  • could be a problem with the adapter: mysql2 ?? – rdiazroman Mar 27 '14 at 12:03
  • How do you check if your table is created? Through phpMyAdmin or through the Rails console? If you check the console with the command: 'Post', it doesn't show you any tables or table shema. First try to use 'Post.count' and after that 'Post'. This connects the console to the database and gives you information about it. – Lukas Warsitz Mar 27 '14 at 12:13
  • ok! For some reason the table exist in Rails console (sqlite): sqlite> select * from posts; sqlite> but not in mysql – rdiazroman Mar 27 '14 at 12:28
1

Could you please tell which OS you got? Delete the line:

 socket: /tmp/mysql.sock

and run:

db:migrate

Give the output of:

db:migrate:status

If this is not working for you, you could also try to add:

host: 127.0.0.1

to your database.yml file

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Kei9Ahfu
  • 47
  • 7
  • ok thanks. Table IS created in sqlite3 (previous config I used). The problem then is...why it is not created in mysql? – rdiazroman Mar 27 '14 at 12:36
  • Maybe you need to remove gem 'sqlite3' in your Gemfile then run bundle install again, so that rails will only detect mysql2 in your Gemfile :) – Romans 8.38-39 Jun 02 '14 at 04:16
1

If nothing stated above works please do check your schema.rb for migration contents. If migration contents are already there then just do the below command in production:

rails db:schema:load RAILS_ENV=production.