Rails 是怎么回事?更改表方法?
我一直在开发中使用 sqlite3 数据库,并且我的应用程序已经变得足够复杂,以至于使用起来有点慢。
我刚刚切换到 MySQL 并运行 rake db:create ; rake db:migrate ,我的其中一个迁移失败,并出现以下错误消息:
undefined method `alter_table` for #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb6e6088c>
我快速谷歌了一下,但没有发现任何结果。然后我检查了 API,没有记录方法 alter_table
。然而,它确实可以与 sqlite3 一起使用!
这是我的迁移:
class AddSettingsToUsers < ActiveRecord::Migration
def self.up
alter_table :users do |t|
t.text signature
...
end
end
...
end
这在 sqlite3 中按预期工作。
我要疯了吗?我是否刚刚发明了这种方法,而它恰好是一个未记录的功能,仅适用于受支持的数据库的子集?
有人对此有一些见解吗?
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??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人提到的,这可能是仅用于 sqlite 的方法。
该文档提到了change_table,因此请使用它,它应该以相同的方式工作:
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:
来说是独一无二的,
它似乎对 sqlite ./lib/active_record/connection_adapters/sqlite_adapter.rb
使用 change_column 方法可能会更安全,因为它抽象了 alter table 方法。
It seems to be unique just to sqlite
./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.
这显示有一个
alter_table
受保护方法。This shows there's an
alter_table
protected method in the Sqlite adapter.关于常规表达式操作,我有不同的 java 适配器到 sqlite 的处理程序。
也许 Rails 并不能完全统治适配器。
I had different handlers for java adapter to sqlite regarding regular expresion manipulation.
Maybe rails does not completly rule over adaptor.