Storing a directory structure in database

后端 未结 3 956
感情败类
感情败类 2021-02-06 05:06

In my rails app a user can have a directory structure which has folders and files in sub-folders. Which is the best way to store such data ??
Also, which database offers bes

3条回答
  •  情话喂你
    2021-02-06 05:15

    You can store a directory tree in a single table using any SQL database, by making the table self-referential. A good example is the Windows Installer's Directory table, where you will see a structure like this:

    • Directory = primary key id field, typically an integer
    • Directory_Parent = "foreign key" id field, which points to the id of another Directory in the same table
    • Value = string containing the directory/folder name

    Your file table would then have a foreign key referencing the Directory id. To find the full path, you must follow it up the chain and build up the path from the end (right), tacking each parent directory onto the front (left). For example, the file would point to Directory id '4' with the Value 'subfolder', then you fetch the parent's value 'folder', then the parents value again until you get to the root, creating a path like /root/folder/subfolder/filename.

提交回复
热议问题