Rails 是怎么回事?更改表方法?

发布于 2024-08-16 07:55:58 字数 663 浏览 2 评论 0原文

我一直在开发中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-08-23 07:55:58

正如其他人提到的,这可能是仅用于 sqlite 的方法。
该文档提到了change_table,因此请使用它,它应该以相同的方式工作:

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

  ...

end

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
梦里梦着梦中梦 2024-08-23 07:55:58

来说是独一无二的,

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

它似乎对 sqlite ./lib/active_record/connection_adapters/sqlite_adapter.rb

使用 change_column 方法可能会更安全,因为它抽象了 alter table 方法。

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.

我不咬妳我踢妳 2024-08-23 07:55:58

显示有一个 alter_table 受保护方法。

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

海夕 2024-08-23 07:55:58

关于常规表达式操作,我有不同的 java 适配器到 sqlite 的处理程序。
也许 Rails 并不能完全统治适配器。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文