What's the deal with Rails' alter_table method?
-
21-09-2019 - |
문제
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??
해결책
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
다른 팁
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.
This shows there's an alter_table
protected method in the Sqlite adapter.
I had different handlers for java adapter to sqlite regarding regular expresion manipulation. Maybe rails does not completly rule over adaptor.