I am reading this book "RailSpace" and am trying to learn rails. However I have been stuck for 2 days on this particular issue.
I've made a user model but when I go to migrate it I keep getting errors. Here's the code I have.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :screen_name, :string
t.column :email, :string
t.column :password, :string
end
end
def self.down
drop_table :users
end
end
It keeps telling me
rake aborted!
== 1 CreateUsers: migrating ===================================================
-- create_table(:users)
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) auto_increment PRIMARY KEY, `screen_name` varchar(255), `email` varchar(255), `password` varchar(255)) ENGINE=InnoDB/Users/coreyholmes/RubymineProjects/worklink/db/migrate/001_create_users.rb:4:in `up'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) auto_increment PRIMARY KEY, `screen_name` varchar(255), `email` varchar(255), `password` varchar(255)) ENGINE=InnoDB
/Users/coreyholmes/RubymineProjects/worklink/db/migrate/001_create_users.rb:4:in `up'
-e:1:in `<main>'
Mysql2::Error: Table 'users' already exists
/Users/coreyholmes/RubymineProjects/worklink/db/migrate/001_create_users.rb:4:in `up'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
What am I doing wrong here? Everything? I really want to learn Rails haha.
As the error says, you're trying to create a table users
that already exists. Is this the only migration you have? If not, make sure you didn't create users
in a previous migration. Also, check your config/database.yml file to ensure that your development database name is correct.
If this is your first migration, you're probably using a database that already has data in it. Assuming its a development database, you should be able to drop it, i.e. destroy it, and create it from scratch. You can drop your database, create it anew, and run migrations with:
rake db:drop
rake db:create
rake db:migrate