0

第一的

ruby script/generate model Buyer id:integer name:string 

生成买方模型后,我做了

rake db:migrate

它工作正常。

1天后,我执行了以下命令

ruby script/generate model Seller id:integer seller_name:string

生成卖方模型后,我做了

rake db:migrate

我收到一个错误,买家表已经存在。为什么?我们有不同的时间戳文件。

class CreateBuyer < ActiveRecord::Migration
  def self.up
    create_table :buyer do |t|
      t.string :name
      t.text :description
      t.decimal :price
      t.integer :seller_id
      t.string :email
      t.string :img_url

      t.timestamps
    end
  end

  def self.down
    drop_table :ads
  end
end

另一个是

class CreateSellers < ActiveRecord::Migration
  def self.up
    create_table :sellers do |t|
      t.integer :nos
      t.decimal :tsv
      t.decimal :avg_price

      t.timestamps
    end
  end

  def self.down
    drop_table :sellers
  end
end

我使用 Rails 2.3.11 和 rake 0.8.7

4

1 回答 1

1

您确定在运行第一次迁移时没有产生错误吗?如果在运行迁移时遇到错误,已经运行的部分仍将在数据库中,但schema_migrations不会使用迁移时间戳进行更新。因此,下次您尝试运行迁移时,它会尝试重新运行失败迁移的第一部分,这将产生错误,因为它已经运行过。

更新:如果您查看您添加的错误输出(顺便说一下,请添加到问题而不是评论,因此它已格式化并包含整个内容)您可以看到第一个Execute db:migrate正在运行迁移CreateBuyer。这确认了您的迁移在您第一次运行时未完成,或者此后未成功回滚。要解决此问题,请手动删除buyer表,然后重新运行迁移。

CreateBuyers请注意,您的迁移中至少存在几个问题:

  1. 表名应该是buyers(复数)而不是buyer(单数)
  2. 迁移的down一部分是删除表ads而不是buyers

实际上,那里的第二个问题可以解释为什么您现在无法运行迁移。如果您回滚CreateBuyers迁移,它会删除您的ads表并留buyers在原地。

于 2011-08-23T23:04:11.563 回答