nfm
nfm

Reputation: 20737

What's the deal with Rails' alter_table method?

I've been using sqlite3 for my database under development and my app has gotten complex enough that it's a bit slow to work with.

I've just switched to MySQL and run rake db:create ; rake db:migrate and one of my migrations failed with the following error message:

undefined method `alter_table` for #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb6e6088c>

I've had a quick google and turned up nothing. Then I checked the API and there is no documented method alter_table. However, it does work with sqlite3!

Here's my migration:

class AddSettingsToUsers < ActiveRecord::Migration
  def self.up
    alter_table :users do |t|
      t.text signature
      ...
    end
  end

  ...

end

This works as expected with sqlite3.

Am I going crazy? Did I just invent this method and it happened to be an undocumented feature that works only on a subset of supported databases?

Does anyone have some insight on this??

Upvotes: 1

Views: 1163

Answers (4)

profimedica
profimedica

Reputation: 2840

I had different handlers for java adapter to sqlite regarding regular expresion manipulation. Maybe rails does not completly rule over adaptor.

Upvotes: 0

Jimmy Stenke
Jimmy Stenke

Reputation: 11220

As the others mentioned, this is probably a method used only for sqlite. The documention mentions change_table, so use that one instead, it should work the same way:

class AddSettingsToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.text :signature
      ...
    end
  end

  ...

end

Upvotes: 3

Rilindo
Rilindo

Reputation: 1796

It seems to be unique just to sqlite

yvaine:activerecord-2.3.5 root# find . -type f -exec grep -l alter_table {} \;

./lib/active_record/connection_adapters/sqlite_adapter.rb

It'll probably be safer just to use the change_column method instead, as that abstracts the alter table method.

Upvotes: 2

JRL
JRL

Reputation: 78033

This shows there's an alter_table protected method in the Sqlite adapter.

Upvotes: 1

Related Questions