代码之家  ›  专栏  ›  技术社区  ›  nfm

Rails的alter_table方法有什么好处?

  •  1
  • nfm  · 技术社区  · 16 年前

    我刚刚切换到MySQL并运行 rake db:create ; rake db:migrate

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

    alter_table . 但是,它确实可以与sqlite3一起使用!

    以下是我的迁移:

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

    这与sqlite3的预期效果一样。

    有人对此有什么见解吗??

    4 回复  |  直到 16 年前
        1
  •  3
  •   Jimmy Stenke    16 年前

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

    class AddSettingsToUsers < ActiveRecord::Migration
      def self.up
        change_table :users do |t|
          t.text :signature
          ...
        end
      end
    
      ...
    
    end
    
        2
  •  2
  •   Rilindo    16 年前

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

    使用change\u column方法可能更安全,因为它抽象了altertable方法。

        3
  •  1
  •   JRL    16 年前

    This 显示有一个 alter_table Sqlite适配器中受保护的方法。

        4
  •  0
  •   profimedica    14 年前

    也许rails并不完全控制适配器。