自加入我自联接称为模型部分:导轨 - 在PostgreSQL
class Section < ApplicationRecord
belongs_to :offer
# Self joins:
has_many :child_sections, class_name: "Section", foreign_key: "parent_section_id"
belongs_to :parent_section, class_name: "Section", optional: true
end
与移民文件:
class CreateSections < ActiveRecord::Migration[5.0]
def change
create_table :sections do |t|
t.string :name
t.references :offer, foreign_key: true
t.references :parent_section, foreign_key: true
t.timestamps
end
end
end
使用MySQL被罚款,但后来我放弃了数据库,改变了他们postresql(所以他们是heroku友好),并创建新的。尝试rails db:migrate
后有错误说:
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "parent_sections" does not exist
可能发生了什么? mysql和postgresql中的自连接有什么区别吗?
真是一个伟大的答案!我一回家就会尝试。我想补充的是,在执行db:reset之后,错误消失了,但仍然无法在之后进行迁移。 – Ancinek
@Ancinek确保接受答案,如果这对你有用 - 这是一个很好的做法和SO的“好做”接受解决你的问题的答案。通过这样做,您既奖励向您提供解决方案**的用户,又让该社区知道问题已解决。 –