-
Notifications
You must be signed in to change notification settings - Fork 8
Database Schema
The Flet-Chat application uses a relational database to store user data, chats, messages, and related information. The schema is designed to support the core functionalities of the chat application while maintaining data integrity and efficiency.
Overview:

The users table stores information about registered users.
| Column | Type | Constraints |
|---|---|---|
| id | Integer | Primary Key, Auto-increment |
| username | String | Unique, Indexed |
| String | Unique, Indexed | |
| hashed_password | String | |
| created_at | DateTime | Default: current timestamp |
| is_active | Boolean | Default: True |
The chats table represents individual chat rooms or conversations.
| Column | Type | Constraints |
|---|---|---|
| id | Integer | Primary Key, Auto-increment |
| name | String | |
| created_at | DateTime | Default: current timestamp |
The messages table stores all messages sent within chats.
| Column | Type | Constraints |
|---|---|---|
| id | Integer | Primary Key, Auto-increment |
| content | String | |
| created_at | DateTime | Default: current timestamp |
| updated_at | DateTime | On update: current timestamp |
| is_deleted | Boolean | Default: False |
| chat_id | Integer | Foreign Key (chats.id) |
| user_id | Integer | Foreign Key (users.id) |
The tokens table manages authentication tokens for users.
| Column | Type | Constraints |
|---|---|---|
| id | Integer | Primary Key, Auto-increment |
| access_token | String | Unique, Indexed |
| refresh_token | String | Unique, Indexed |
| token_type | String | |
| expires_at | DateTime | |
| user_id | Integer | Foreign Key (users.id) |
The message_statuses table tracks the read status of messages for each user.
| Column | Type | Constraints |
|---|---|---|
| id | Integer | Primary Key, Auto-increment |
| message_id | Integer | Foreign Key (messages.id) |
| user_id | Integer | Foreign Key (users.id) |
| is_read | Boolean | Default: False |
| read_at | DateTime | Nullable |
The chat_members table is a junction table that manages the many-to-many relationship between users and chats.
| Column | Type | Constraints |
|---|---|---|
| chat_id | Integer | Primary Key, Foreign Key (chats.id) |
| user_id | Integer | Primary Key, Foreign Key (users.id) |
- A User can be a member of multiple Chats, and a Chat can have multiple Users (Many-to-Many relationship through
chat_members). - A User can send multiple Messages, but each Message belongs to one User (One-to-Many).
- A Chat can contain multiple Messages, but each Message belongs to one Chat (One-to-Many).
- A User can have multiple Tokens, but each Token belongs to one User (One-to-Many).
- A Message can have multiple MessageStatuses (one for each user in the chat), and each MessageStatus belongs to one Message and one User (One-to-Many for both relationships).
-
users: Indexes onid,username, andemailfor faster lookups. -
chats: Index onid. -
messages: Indexes onid,chat_id, anduser_idfor efficient message retrieval. -
tokens: Indexes onid,access_token, andrefresh_tokenfor quick token validation. -
message_statuses: Indexes onid,message_id, anduser_idfor efficient status checks.
- Foreign key constraints ensure referential integrity between related tables.
- Unique constraints on
users.username,users.email,tokens.access_token, andtokens.refresh_tokenprevent duplicate entries. - The composite primary key on
chat_members(chat_id, user_id) ensures a user can only be added to a chat once.
This schema design supports the core functionalities of the chat application, including user management, chat creation and participation, message sending and reading, and token-based authentication. The use of foreign keys and indexes helps maintain data integrity and query performance.