from sqlalchemy import Column, Integer, String, ForeignKey, Date
from sqlalchemy.orm import relationship, declarative_base
from sqlalchemy.dialects.postgresql import ARRAY, JSONB
from sqlalchemy.ext.mutable import MutableList
Base = declarative_base()
# reviews db depends on username as a foreign key, so we define it first here for
# it to use as a dependency, otherwise if the reviews table is attempted to be
# created first we would have a users table not found error
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True, nullable=False)
email = Column(String, unique=True, index=True, nullable=False)
hashed_password = Column(String, nullable=False)
dob = Column(Date, nullable=False)
country_of_origin = Column(String, nullable=False)
followers = Column(MutableList.as_mutable(ARRAY(String)), default=[])
following = Column(MutableList.as_mutable(ARRAY(String)), default=[])
bio = Column(String, nullable=True)
profile_picture = Column(String, nullable=True)
reviews = relationship("Review", back_populates="user", cascade="all, delete-orphan")
class Review(Base):
__tablename__ = "reviews"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
username = Column(String, ForeignKey("users.username", ondelete="CASCADE"), nullable=False)
location = Column(String, nullable=False)
rating = Column(Integer, nullable=False)
comment = Column(String, nullable=False)
user = relationship("User", back_populates="reviews")