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

当列与表不匹配时,如何可逆地删除表上的\u foreign\u键?

  •  6
  • Chloe  · 技术社区  · 8 年前

    我尝试了以下方法

    remove_foreign_key :users, :asset_types, column: :seek_asset_type_id
    

    但是有个错误


    参数数量错误(给定3,应为1.2)

    文件中说它使用了与 #add_foreign_key .

    Documentation

    add_reference :users, :seek_asset_type, foreign_key: {to_table: :asset_types}
    

    "fk_rails_4dcaa1c59c" FOREIGN KEY (seek_asset_type_id) REFERENCES asset_types(id)
    
    4 回复  |  直到 8 年前
        1
  •  5
  •   Chloe    7 年前

    revert :

    def change
      revert do
        add_reference :users, :seek_asset_type, foreign_key: { to_table: :asset_types }
      end
    end
    

    def change
      revert AddAssetTypeReferenceToUsers 
      # I made up this migration name, 
      # so please fill in the appropriate name
    end
    
        2
  •  1
  •   Abe Voelker    6 年前

    这似乎是 remove_foreign_key ,但您可以通过定义 add_foreign_key reversible :

    reversible do |dir|
      dir.up do
        remove_foreign_key :users, column: :seek_asset_type
      end
      dir.down do
        add_foreign_key :users, :asset_types, column: :seek_asset_type
      end
    end
    
        3
  •  0
  •   Asmita    8 年前

    请删除表名称

    remove\u foreign\u key:users,column::seek\u asset\u type\u id

        4
  •  0
  •   Asmita    8 年前

    试试这个 我已经测试过了,为我工作。