【问题标题】:Why is my Rails DB migration not creating columns?为什么我的 Rails DB 迁移没有创建列?
【发布时间】:2019-03-30 15:18:44
【问题描述】:

我正在尝试使用 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

这与我的模型文件有关吗?我不知道它为什么这样做,我不能发布更多代码,因为我必须在这篇文章中添加更多细节以避免警告我的问题没有足够的细节。

【问题讨论】:

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


    【解决方案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】:

      这里的其他答案是正确的。但是,如果您已经运行了迁移并且没有出现错误,则可能需要回滚它,但如果失败,您将需要在数据库中手动回滚迁移。因为任何迁移都会使用迁移文件名中的版本号增加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
      

      Difference between rake db:migrate db:reset and db:schema:load

      【讨论】:

      • 谢谢,但现在它告诉我,只要我运行迁移,关系就已经存在。
      • 再次阅读我的答案......它试图帮助你,你需要使用文件中的版本号,我的只是一个例子。
      【解决方案3】:

      它没有创建列,因为您在 change 方法中定义了 updown

      试试这个

      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
      

      【讨论】:

        猜你喜欢
        • 2012-10-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2012-09-18
        • 2021-10-27
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多