Newbie Migrations issue

I'm taking a crack at Rails and have run into a snag fairly early on.
I read a tutorial that showed me how to use migrations and everything
was going fine until I actually tried to use what I'd written.

First off, I'm using Windows XP, MySQL 5.0.19, rails 2.2, ruby 1.8.5.

I used this little script to verify the connection to the database was
working:

http://blog.caboo.se/articles/2005/8/4/test-your-database-connection-in-rails

The migration file is pretty simple. So simple it's probably not the
problem.

class BankDb < ActiveRecord::Migration
  def self.up
        create_table "accounts" do |t|
                t.column "id", :integer
                t.column "name", :string
        end
  end
  def self.down
        drop_table "accounts"
  end
end

g:\sites\bank\db\migrate>rake db:migrate VERSION=1
(in g:/sites/bank)
== BankDb: migrating

Gabriel wrote:

I'm taking a crack at Rails and have run into a snag fairly early on.
I read a tutorial that showed me how to use migrations and everything
was going fine until I actually tried to use what I'd written.

First off, I'm using Windows XP, MySQL 5.0.19, rails 2.2, ruby 1.8.5.

I used this little script to verify the connection to the database was
working:

http://blog.caboo.se/articles/2005/8/4/test-your-database-connection-in-rails

The migration file is pretty simple. So simple it's probably not the
problem.

class BankDb < ActiveRecord::Migration
  def self.up
        create_table "accounts" do |t|
                t.column "id", :integer
                t.column "name", :string
        end
  end
  def self.down
        drop_table "accounts"
  end
end

g:\sites\bank\db\migrate>rake db:migrate VERSION=1
(in g:/sites/bank)
== BankDb: migrating

-- create_table("accounts")
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '(
11), `name` varchar(255) DEFAULT NULL) ENGINE=InnoDB' at line 1:
CREATE TABLE accounts (`id` int(11) DEFAULT NULL auto_increment
PRIMARY KEY(11), `nam
e` varchar(255) DEFAULT NULL) ENGINE=InnoDB

I logged into MySQL and tried the SQL statement generated but changed
"PRIMARY KEY(11)" to simply "PRIMARY KEY" and it worked. I assume
there's a version mismatch with the syntax Rails expects MySQL to
understand and what my version of MySQL really does understand.

I haven't had much luck with web searches for things like ``rails
migrations primary key`` or ``migrations mysql syntax issues`` or,
well, anything else I thought up. I assume I just need to update
"migrations" but googling ``rails upgrade migrations`` and the like
doesn't do much good either. Obviously it's something obvious that all
rails guys know and don't have to look up. :wink:

What am I missing? =(

Try taking out the t.column "id" line. Rails will create it by default unless you specifically tell it not to so having it there is redundant and may be causing a problem. Also the convention is to use symbols for the column names as in:

t.column :name, :string

Try taking out the t.column "id" line.

=O

It's the little things in life, no?

Thank you Michael!

Rails automatically adds an 'id' field for you, so you don't have to
declare it yourself. Try the following instead:

class BankDb < ActiveRecord::Migration
  def self.up
        create_table "accounts" do |t|
                t.column "name", :string
        end
  end
  def self.down
        drop_table "accounts"
  end
end