【问题标题】:PG::DependentObjectsStillExist: ERROR: cannot drop column xxx_id of xxxxx because other objects depend on itPG::DependentObjectsStillExist: 错误: 不能删除 xxxxx 的列 xxx_id 因为其他对象依赖它
【发布时间】:2020-12-15 02:19:17
【问题描述】:

我正在尝试从名为 subscriptions 的表中删除外键 user_id

模型关联就像

#user.rb

has_many :subscriptions
has_many :orders_through_vehicle, through: :subscriptions, source: :line_items
accepts_nested_attributes_for :subscriptions

  #subscription.rb

  # Indexes
  #
  #  idx_user_id (user_id)

  belongs_to :user, required: true

当我运行迁移时, remove_column :subscriptions, :user_id,它会抛出以下错误:

PG::DependentObjectsStillExist: ERROR:  cannot drop column user_id of table subscriptions because other objects depend on it
DETAIL:  view vw_customer_size depends on column user_id of table subscriptions
view vw_subscriptions_daily_report depends on column user_id of table subscriptions
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

我不关心这些 Postgres 视图。我是否也必须删除它们,我该如何使用 rails 迁移呢?

谢谢

【问题讨论】:

    标签: ruby-on-rails postgresql foreign-keys rails-migrations


    【解决方案1】:

    我能够解决这个问题。这里需要先去掉两个数据库视图,

        execute <<-SQL
          drop view if exists vw_customer_size
        SQL
    
        execute <<-SQL
          drop view if exists vw_subscriptions_daily_report
        SQL
    
        remove_column :subscriptions, :user_id
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2017-04-14
      • 2013-06-06
      • 2018-09-01
      • 2016-05-22
      • 2020-12-27
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多