在帖子,评论和个人资料页面上上传文件的数据库设计

时间:2022-01-28 12:50:04

I am new to web development and am trying to implement a website where users can upload photos and other files (i.e. .doc, .xls, .ppt, .txt, .pdf,etc...) on profile pages, posts and comments. I am using an s3 bucket for the file storage and will use a mysql database to store the file urls and other associative data. What I am confused about is the following:

我是网络开发的新手,我正在尝试实现一个网站,用户可以在个人资料页面,帖子和评论上上传照片和其他文件(例如.doc,.xls,.ppt,.txt,.pdf等...) 。我正在使用s3存储桶进行文件存储,并将使用mysql数据库来存储文件URL和其他关联数据。我困惑的是以下内容:

Which of these is the best idea?

哪一个是最好的主意?

a) create a files tables for each of the following: profiles,posts,comments and then get all the files associated with the id(A FK) of the specified object(the post, the comment, or the profile).

a)为以下各项创建文件表:配置文件,帖子,注释,然后获取与指定对象(帖子,评论或配置文件)的id(A FK)相关联的所有文件。

b) create one files table that has a field named "type" which can be either "profile","post","comment" and a field named "id" which is a FK of the the id of the table specified in the "type" field.

b)创建一个具有名为“type”的字段的文件表,该字段可以是“profile”,“post”,“comment”和名为“id”的字段,该字段是在表中指定的表的id的FK。 “类型”字段。

c) use a completely different schema that anyone finds more advantageous

c)使用完全不同的模式,任何人都发现更有利

EDIT: I want each file to be associated to the uploader(user_id) but also attached to the entity it was uploaded on(i.e. profile,post,comment)

编辑:我希望每个文件与上传者(user_id)相关联,但也附加到上传的实体(即个人资料,帖子,评论)

3 个解决方案

#1


2  

I would create one table Files like

我会创建一个表文件像

Files
|id|file_path|...(other information about the file)

If a Post, Comment and Profile can contain only one file you can add the file_id directly in the tables like

如果帖子,评论和配置文件只能包含一个文件,您可以直接在表格中添加file_id

Posts
|id|data|...|file_id|

If they can have more then one file you'll need a mapping table like:

如果他们可以有多个文件,你需要一个映射表,如:

Posts
|id|data|...

Posts_Files_Mapping
|post_id|file_id|

#2


0  

As you say you are new to web development.

正如您所说,您是Web开发的新手。

Pehaps you should consider skipping relational type database like mysql and use a "NOSQL" database like mongodb.

你应该考虑跳过像mysql这样的关系类型数据库并使用像mongodb这样的“NOSQL”数据库。

There many benefits to mongodb but one of the major one is the concept of "scheme-less" database which allows you to store different types of data within the same collection/table.

mongodb有许多好处,但主要的一个是“无方案”数据库的概念,它允许您在同一个集合/表中存储不同类型的数据。

But don't think this is a easy way out, you still have to have a good understanding of the best way to model your data.

但是不要认为这是一个简单的方法,你仍然需要很好地理解建模数据的最佳方法。

#3


0  

Do files always belong to the same "thing", in your case probably a User, or a UserProfile or a Person?

文件是否总是属于同一个“东西”,在您的情况下可能是User,UserProfile或Person?

In that case, I would define a set of generic fields that apply to all types of files and include a 'type' field to distinguish between file types.

在这种情况下,我将定义一组适用于所有类型文件的通用字段,并包含一个“类型”字段来区分文件类型。

Something like this could work

像这样的东西可以工作

- User/Person/UserProfile/Account
userId (PK)
firstname
lastname

- File
fileId (PK)
title
description
fileType
userId (FK)
size

Now, this is a model for if you want your files linked to a User directly, but you might want to have them linked to the Posts they were placed, like in Facebook. In that case I would embed a link to the file as part of the post's contents and link the post to the user.

现在,这是一个模型,如果您希望您的文件直接链接到用户,但您可能希望将它们链接到它们被放置的帖子,就像在Facebook中一样。在这种情况下,我会将文件的链接作为帖子内容的一部分嵌入,并将帖子链接到用户。

It depends on what your needs are.

这取决于您的需求。

#1


2  

I would create one table Files like

我会创建一个表文件像

Files
|id|file_path|...(other information about the file)

If a Post, Comment and Profile can contain only one file you can add the file_id directly in the tables like

如果帖子,评论和配置文件只能包含一个文件,您可以直接在表格中添加file_id

Posts
|id|data|...|file_id|

If they can have more then one file you'll need a mapping table like:

如果他们可以有多个文件,你需要一个映射表,如:

Posts
|id|data|...

Posts_Files_Mapping
|post_id|file_id|

#2


0  

As you say you are new to web development.

正如您所说,您是Web开发的新手。

Pehaps you should consider skipping relational type database like mysql and use a "NOSQL" database like mongodb.

你应该考虑跳过像mysql这样的关系类型数据库并使用像mongodb这样的“NOSQL”数据库。

There many benefits to mongodb but one of the major one is the concept of "scheme-less" database which allows you to store different types of data within the same collection/table.

mongodb有许多好处,但主要的一个是“无方案”数据库的概念,它允许您在同一个集合/表中存储不同类型的数据。

But don't think this is a easy way out, you still have to have a good understanding of the best way to model your data.

但是不要认为这是一个简单的方法,你仍然需要很好地理解建模数据的最佳方法。

#3


0  

Do files always belong to the same "thing", in your case probably a User, or a UserProfile or a Person?

文件是否总是属于同一个“东西”,在您的情况下可能是User,UserProfile或Person?

In that case, I would define a set of generic fields that apply to all types of files and include a 'type' field to distinguish between file types.

在这种情况下,我将定义一组适用于所有类型文件的通用字段,并包含一个“类型”字段来区分文件类型。

Something like this could work

像这样的东西可以工作

- User/Person/UserProfile/Account
userId (PK)
firstname
lastname

- File
fileId (PK)
title
description
fileType
userId (FK)
size

Now, this is a model for if you want your files linked to a User directly, but you might want to have them linked to the Posts they were placed, like in Facebook. In that case I would embed a link to the file as part of the post's contents and link the post to the user.

现在,这是一个模型,如果您希望您的文件直接链接到用户,但您可能希望将它们链接到它们被放置的帖子,就像在Facebook中一样。在这种情况下,我会将文件的链接作为帖子内容的一部分嵌入,并将帖子链接到用户。

It depends on what your needs are.

这取决于您的需求。