class Record2person(db.Model):
    __tablename__ = "record2person"
    id = db.Column('id', db.Integer, primary_key=True)
    record_id = db.Column('record_id', db.Unicode, db.ForeignKey('record.id'))
    person_id = db.Column('person_id', db.Integer, db.ForeignKey('person.id'))
    buried = db.Column('buried', db.Boolean, default=False)

    person = db.relationship('Person',
                             foreign_keys=person_id,
                             backref='record')
    record = db.relationship('Record', foreign_keys=record_id)

    def __repr__(self):
        return self.record.id
class Person2person(db.Model):
    __tablename__ = "person2person"
    id = db.Column('id', db.Integer, primary_key=True)
    p1_id = db.Column('p1_id', db.Integer, db.ForeignKey('person.id'))
    p2_id = db.Column('p2_id', db.Integer, db.ForeignKey('person.id'))
    relationinfo_id = db.Column('relationinfo_id', db.Integer,
                                db.ForeignKey('relationinfo.id'))

    relationinfo = db.relationship('Relationinfo',
                                   foreign_keys=relationinfo_id)

    person1 = db.relationship('Person',
                              foreign_keys=p1_id,
                              backref='related_to')
    person2 = db.relationship('Person', foreign_keys=p2_id)

    def __repr__(self):

        return f"{self.person2} ({self.relationinfo})"
class Relationinfo(db.Model):
    __tablename__ = "relationinfo"
    id = db.Column('id', db.Integer, primary_key=True)
    name = db.deferred(db.Column('name', db.Text))
    parent_category_id = db.Column('parent_category_id', db.Integer,
                                   db.ForeignKey('relationinfo.id'))

    parent_category = db.relationship('Relationinfo',
                                      foreign_keys=parent_category_id)

    def __repr__(self):
        return self.name
class Record(db.Model):
    __tablename__ = "record"
    id = db.Column('id', db.Unicode, primary_key=True)
    inventory = db.Column('inventory', db.Unicode)
    date = db.Column('date', db.Date)
    church_id = db.Column('church_id', db.Integer, db.ForeignKey('church.id'))
    source = db.Column('source', db.Unicode)
    scan_id = db.Column('scan_id', db.Integer, db.ForeignKey('scan.id'))
    relationinfo_id = db.Column('relationinfo_id', db.Integer,
                                db.ForeignKey('relationinfo.id'))

    church = db.relationship('Church',
                             foreign_keys=church_id,
                             backref='records')
    scan = db.relationship('Scan', foreign_keys=scan_id, backref='records')
    relationinfo = db.relationship('Relationinfo',
                                   foreign_keys=relationinfo_id,
                                   backref='records')

    registered = db.relationship(
        "Person",
        secondary='record2person',
        primaryjoin=
        "and_(Record.id==Record2person.record_id, Record2person.buried==False)"
    )
    buried = db.relationship(
        "Person",
        secondary='record2person',
        primaryjoin=
        "and_(Record.id==Record2person.record_id, Record2person.buried==True)")

    # registered = association_proxy('registered_p', 'name')
    # buried = association_proxy('buried_p', 'name')

    def __repr__(self):
        return self.id
class Person2personname(db.Model):
    __tablename__ = "person2personname"
    id = db.Column('id', db.Integer, primary_key=True)
    person_id = db.Column('person_id', db.Integer, db.ForeignKey('person.id'))
    personname_id = db.Column('personname_id', db.Integer,
                              db.ForeignKey('personname.id'))