<commit_before>from collections import OrderedDict

from sqlalchemy import Column, Integer, ForeignKey, Boolean, String, Index
from sqlalchemy.orm import relationship

from radar.database import db
from radar.models.common import MetaModelMixin, uuid_pk_column, patient_id_column, patient_relationship


RELATIVES = OrderedDict([
    (1, 'Mother'),
    (2, 'Father'),
    (3, 'Sister'),
    (4, 'Brother'),
    (5, 'Grandmother - Maternal'),
    (6, 'Grandmother - Paternal'),
    (15, 'Grandfather - Maternal'),
    (16, 'Grandfather - Paternal'),
    (7, 'Aunt - Maternal'),
    (8, 'Aunt - Paternal'),
    (9, 'Uncle - Maternal'),
    (10, 'Uncle - Paternal'),
    (11, 'Cousin - Maternal'),
    (12, 'Cousin - Paternal'),
    (13, 'Half Sister'),
    (14, 'Half Brother'),
])


class FamilyHistory(db.Model, MetaModelMixin):
    __tablename__ = 'family_history'

    id = uuid_pk_column()

    patient_id = patient_id_column()
    patient = patient_relationship('family_history')

    cohort_id = Column(Integer, ForeignKey('cohorts.id'), nullable=False)
    cohort = relationship('Cohort')

    parental_consanguinity = Column(Boolean, nullable=False)
    family_history = Column(Boolean, nullable=False)
    other_family_history = Column(String)

Index('family_history_patient_id_idx', FamilyHistory.patient_id)
Index('family_history_cohort_id_idx', FamilyHistory.cohort_id)


class FamilyHistoryRelative(db.Model):
    __tablename__ = 'family_history_relatives'

    id = Column(Integer, primary_key=True)

    family_history_id = Column(Integer, ForeignKey('family_history.id'), nullable=False)
    family_history = relationship('FamilyHistory')

    relative = Column(Integer, nullable=False)

    # TODO null when patient is deleted
    patient_id = Column(Integer, ForeignKey('patients.id'))
    patient = relationship('Patient')

Index('family_history_relatives_family_history_id_idx', FamilyHistoryRelative.family_history_id)<commit_msg>Fix family history foreign key<commit_after>from collections import OrderedDict

from sqlalchemy import Column, Integer, ForeignKey, Boolean, String, Index
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship

from radar.database import db
from radar.models.common import MetaModelMixin, uuid_pk_column, patient_id_column, patient_relationship


RELATIVES = OrderedDict([
    (1, 'Mother'),
    (2, 'Father'),
    (3, 'Sister'),
    (4, 'Brother'),
    (5, 'Grandmother - Maternal'),
    (6, 'Grandmother - Paternal'),
    (15, 'Grandfather - Maternal'),
    (16, 'Grandfather - Paternal'),
    (7, 'Aunt - Maternal'),
    (8, 'Aunt - Paternal'),
    (9, 'Uncle - Maternal'),
    (10, 'Uncle - Paternal'),
    (11, 'Cousin - Maternal'),
    (12, 'Cousin - Paternal'),
    (13, 'Half Sister'),
    (14, 'Half Brother'),
])


class FamilyHistory(db.Model, MetaModelMixin):
    __tablename__ = 'family_history'

    id = uuid_pk_column()

    patient_id = patient_id_column()
    patient = patient_relationship('family_history')

    cohort_id = Column(Integer, ForeignKey('cohorts.id'), nullable=False)
    cohort = relationship('Cohort')

    parental_consanguinity = Column(Boolean, nullable=False)
    family_history = Column(Boolean, nullable=False)
    other_family_history = Column(String)

Index('family_history_patient_id_idx', FamilyHistory.patient_id)
Index('family_history_cohort_id_idx', FamilyHistory.cohort_id)


class FamilyHistoryRelative(db.Model):
    __tablename__ = 'family_history_relatives'

    id = Column(Integer, primary_key=True)

    family_history_id = Column(UUID, ForeignKey('family_history.id'), nullable=False)
    family_history = relationship('FamilyHistory')

    relative = Column(Integer, nullable=False)

    # TODO null when patient is deleted
    patient_id = Column(Integer, ForeignKey('patients.id'))
    patient = relationship('Patient')

Index('family_history_relatives_family_history_id_idx', FamilyHistoryRelative.family_history_id)
{'fraction_non_alphanumeric': 0.12165572942958101, 'fraction_numerical': 0.011610297829379102, 'mean_word_length': 4.871111111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6327855', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1230, 'n_words': 271}