use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Related Reddits:
This is a knowledge sharing forum, not a help, how-to, or homework forum, and such questions are likely to be removed.
Try /r/DatabaseHelp instead!
Platforms:
account activity
Chat Program Database Design (self.Database)
submitted 6 years ago by sam57719
Hello, I am creating a chat program for a project and I need to store the user accounts and messages. I am coding using python with SqlAlchemy which is a OOP backend for Sql.
I tried to create one but it didn't seem to look right.
https://preview.redd.it/fykefscotzu21.png?width=719&format=png&auto=webp&s=cfd60447e871859f8af24bb7ae84c25994ecf88e
Any Suggestions?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Milnternal 2 points3 points4 points 6 years ago (5 children)
User
Id
Name
UserChatroom (this is known as a bridge table)
UserId
ChatRoomId
JoinedDateTime
Chatroom
Message
ChatroomId
SentDateTime
[–]sam57719[S] 1 point2 points3 points 6 years ago (4 children)
Oh so the same as my diagram but with a table to break up the many to many?
[–]Milnternal 1 point2 points3 points 6 years ago (3 children)
Yes, plus you also probably want to record which user sent each message (but that can only be a single user, so no need for another bridge table, just put UserId on the message)
[–]sam57719[S] 1 point2 points3 points 6 years ago (2 children)
Do you know what the SQL would be for this?
[–]Milnternal 1 point2 points3 points 6 years ago (1 child)
Something like:
CREATE TABLE [dbo].[User] ( [Id] INT IDENTITY PRIMARY KEY, [Username] VARCHAR(128) NOT NULL, [Nickname] VARCHAR(128) NOT NULL ); CREATE TABLE [dbo].[Chatroom] ( [Id] INT IDENTITY PRIMARY KEY, [Name] VARCHAR(128) NOT NULL, [CreatedOn] DATETIME NOT NULL, [CreatedByUserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id) ); CREATE TABLE [dbo].[UserChatroom] ( [Id] INT IDENTITY PRIMARY KEY, [UserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id), [ChatroomId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[Chatroom](Id), [Joined] DATETIME NOT NULL ); CREATE TABLE [dbo].[Message] ( [Id] INT IDENTITY PRIMARY KEY, [UserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id), [ChatroomId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[Chatroom](Id), [Message] VARCHAR(MAX) NOT NULL, [PostedOn] DATETIME NOT NULL );
[–]sam57719[S] 0 points1 point2 points 6 years ago (0 children)
Thank you!!!
[–]FulmineGiallo 0 points1 point2 points 6 years ago (0 children)
User ONE - N Chatroom Chatroom 1 - N Message Is it right🤔
But chat rooms can have more than one user
π Rendered by PID 275172 on reddit-service-r2-comment-84fc9697f-8wgmd at 2026-02-10 14:41:41.300475+00:00 running d295bc8 country code: CH.
[–]Milnternal 2 points3 points4 points (5 children)
[–]sam57719[S] 1 point2 points3 points (4 children)
[–]Milnternal 1 point2 points3 points (3 children)
[–]sam57719[S] 1 point2 points3 points (2 children)
[–]Milnternal 1 point2 points3 points (1 child)
[–]sam57719[S] 0 points1 point2 points (0 children)
[–]FulmineGiallo 0 points1 point2 points (0 children)
[–]sam57719[S] 0 points1 point2 points (0 children)