Skip to content

Commit

Permalink
updated contact contact_types relationship (#142)
Browse files Browse the repository at this point in the history
* updated contact contact_types relationship

* renaming
  • Loading branch information
CurtisMIT authored Mar 13, 2021
1 parent 5274c14 commit 2ebc8bf
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __repr__(self):
db.Column(
"contact_id",
UUID(as_uuid=True),
db.ForeignKey("contact.id"),
db.ForeignKey("contacts.id"),
primary_key=True,
default=uuid.uuid4,
),
Expand Down Expand Up @@ -113,8 +113,28 @@ def __repr__(self):
return "<Project %r>" % self.id


belongs_to = db.Table(
"belongs_to",
db.Column(
"contact_id",
UUID(as_uuid=True),
db.ForeignKey("contacts.id"),
primary_key=True,
default=uuid.uuid4,
),
db.Column(
"contact_type_id",
UUID(as_uuid=True),
db.ForeignKey("contact_types.id"),
primary_key=True,
default=uuid.uuid4,
),
)


# Contact Model
class Contact(db.Model):
__tablename__ = "contacts"
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4())
name = db.Column(db.String(256), nullable=False)
email = db.Column(db.String(256), nullable=False)
Expand All @@ -124,8 +144,11 @@ class Contact(db.Model):
organization = db.Column(db.String(256), nullable=False)
neighbourhood = db.Column(db.String(256))
# many to one realtionship with contact_type
contact_type = db.Column(
UUID(as_uuid=True), db.ForeignKey("contact_types.id"), nullable=False
contact_types = db.relationship(
"ContactType",
secondary=belongs_to,
lazy="subquery",
backref=db.backref("contacts", lazy=True),
)

@property
Expand Down Expand Up @@ -158,7 +181,6 @@ class ContactType(db.Model):
hex_colour = db.Column(db.String(8))
type = db.Column(db.String(64))
description = db.Column(db.String(512))
contacts = db.relationship("Contact", backref="contact_types", lazy=True)

@property
def serialize(self):
Expand Down

0 comments on commit 2ebc8bf

Please sign in to comment.