File size: 1,119 Bytes
24fdbf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.orm import relationship

from database import Base

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    messages = relationship("Message", back_populates="user")

# TODO: Implement the Message SQLAlchemy model. Message should have a primary key, 
# a message attribute to store the content of messages, a type, AI or Human, 
# depending on if it is a user question or an AI response, a timestamp to 
# order by time and a user attribute to get the user instance associated 
# with the message. We also need a user_id that will use the User.id 
# attribute as a foreign key.
class Message(Base):
    __tablename__ = "messages"

    id = Column(Integer, primary_key=True, index=True)
    message = Column(String, index=True)
    type = Column(String) # "AI" or "Human"
    timestamp = Column(DateTime, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))

    user = relationship("User", back_populates="messages")