为什么我的Rails DB迁移无法创建项目?

时间:2018-10-25 22:28:30

标签: ruby-on-rails ruby database postgresql migration

我正在尝试使用PostgreSQL在Rails中进行数据库迁移,但是生成的模式不包含我的任何表定义。我没看到语法有什么问题吗?

这是我运行“ rake db:migrate”后迁移文件之一和生成的模式文件的示例。

迁移文件:

class Fields < ActiveRecord::Migration[5.2]

      def change

        def up



          create_table :fields do |t|

            t.column :totalsalesprsn, :float, :limit => nil, :null => false

            t.column :totaladmkspend, :float, :limit => nil, :null => false

            t.column :totalsalescost, :float, :limit => nil, :null => false

            t.column :miscsales, :float, :limit => nil, :null => false

            t.column :numleads, :float, :limit => nil, :null => false

            t.column :costleads, :float, :limit => nil, :null => false

            t.column :totalsalescost2, :float, :limit => nil, :null => false

            t.column :totalmarketspent, :float, :limit => nil, :null => false

            t.column :numsales, :float, :limit => nil, :null => false

            t.column :averagecost, :float, :limit => nil, :null => false

            t.column :costpersale, :float, :limit => nil, :null => false

            t.column :totalspending, :float, :limit => nil, :null => false

            t.column :totalsalesdonate, :float, :limit => nil, :null => false

            t.column :totalsales, :float, :limit => nil, :null => false

            t.column :pototal, :float, :limit => nil, :null => false

            t.column :posales, :float, :limit => nil, :null => false



            t.column :form_id, :integer

            t.column :created_at, :timestamp

          end

        end



        def down

          drop_table :fields

        end

      end

    end

架构文件:

ActiveRecord::Schema.define(version: 2018_10_25_161515) do



  # These are extensions that must be enabled in order to support this database

  enable_extension "plpgsql"



  create_table "fields", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



  create_table "forms", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



  create_table "tables", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



end

与我的模型文件有关吗?我不知道为什么要这样做,并且我不能发布更多的代码,因为我必须在此帖子中添加更多详细信息,以避免出现警告我的问题没有足够详细信息的警告。

3 个答案:

答案 0 :(得分:5)

这不是创建列,因为您是在up方法内定义downchange

尝试一下

class Fields < ActiveRecord::Migration[5.2]
  def up
    create_table :fields do |t|
      t.column :totalsalesprsn, :float, :limit => nil, :null => false
      t.column :totaladmkspend, :float, :limit => nil, :null => false
      t.column :totalsalescost, :float, :limit => nil, :null => false
      t.column :miscsales, :float, :limit => nil, :null => false
      t.column :numleads, :float, :limit => nil, :null => false
      t.column :costleads, :float, :limit => nil, :null => false
      t.column :totalsalescost2, :float, :limit => nil, :null => false
      t.column :totalmarketspent, :float, :limit => nil, :null => false
      t.column :numsales, :float, :limit => nil, :null => false
      t.column :averagecost, :float, :limit => nil, :null => false
      t.column :costpersale, :float, :limit => nil, :null => false
      t.column :totalspending, :float, :limit => nil, :null => false
      t.column :totalsalesdonate, :float, :limit => nil, :null => false
      t.column :totalsales, :float, :limit => nil, :null => false
      t.column :pototal, :float, :limit => nil, :null => false
      t.column :posales, :float, :limit => nil, :null => false
      t.column :form_id, :integer
      t.column :created_at, :timestamp
    end
  end

  def down
    drop_table :fields
  end
end

请查看有关如何定义迁移的文档here


从文档中

  

change方法是编写迁移的主要方法。它适用于大多数情况,其中Active Record知道如何自动撤消迁移。

因此,您也可以通过执行以下操作来定义迁移

class Fields < ActiveRecord::Migration[5.2]
  def change
    create_table :fields do |t|
      t.column :totalsalesprsn, :float, :limit => nil, :null => false
      t.column :totaladmkspend, :float, :limit => nil, :null => false
      t.column :totalsalescost, :float, :limit => nil, :null => false
      t.column :miscsales, :float, :limit => nil, :null => false
      t.column :numleads, :float, :limit => nil, :null => false
      t.column :costleads, :float, :limit => nil, :null => false
      t.column :totalsalescost2, :float, :limit => nil, :null => false
      t.column :totalmarketspent, :float, :limit => nil, :null => false
      t.column :numsales, :float, :limit => nil, :null => false
      t.column :averagecost, :float, :limit => nil, :null => false
      t.column :costpersale, :float, :limit => nil, :null => false
      t.column :totalspending, :float, :limit => nil, :null => false
      t.column :totalsalesdonate, :float, :limit => nil, :null => false
      t.column :totalsales, :float, :limit => nil, :null => false
      t.column :pototal, :float, :limit => nil, :null => false
      t.column :posales, :float, :limit => nil, :null => false
      t.column :form_id, :integer
      t.column :created_at, :timestamp
    end
  end
end

答案 1 :(得分:1)

您的语法有些错误:

class Fields < ActiveRecord::Migration[5.2]
  def change
    def up
     # your table definition
    end
  end

  def down
    # delete your table
  end
end

Rails足够聪明,可以知道创建表的相反操作是将其删除,因此您无需指定def down

您似乎没有在迁移中显示错误,因此在执行以下操作之前,请从控制台中运行命令rails db:rollback

然后将您的迁移文件更改为以下文件,然后再次运行rails db:migrate

class Fields < ActiveRecord::Migration[5.2]
  def change
   # your table definition
  end
end

答案 2 :(得分:0)

这里的其他答案是正确的。但是,如果您已经运行了迁移并且没有错误,则可能需要回滚,但是如果失败,则需要手动回滚数据库中的迁移。因为任何迁移都会使schema_migrations带有迁移文件名中的版本号。

因此,如果您的迁移文件名类似于20181023191125_fields.rb,则需要执行以下操作:

rails dbconsole
#now you should be in a (pg?) console
DELETE from schema_migrations WHERE version = 20181023191125;
\q # to quit postgres

但是,如果这是一个新项目,并且您有种子,那么按照其他人的指示,在解决了迁移问题之后,从一个新的数据库重新开始可能会更容易。

仅当您精通杀死数据库并重新开始时才这样做!

rake db:setup
# or
rails db:setup

请参见link