Search code examples
database-designflaskflask-sqlalchemyflask-adminflask-security

Flask: Connecting models to files, e.g. profile pictures


If it matters, I use Flask-Security, Flask-SQLAlchemy and Flask-Admin in my application.

How should I design my application so that I can connect a row in the database with one or more files, e.g. a user to their profile pictures?


Solution

  • If only one file/picture is required, it should work by adding another column. But if you want to associate multiple files to a specific user, you can create a new table for the relations:

    files table:
    
    ID   |   USER_ID   |   FILEPATH
    ===================================================
    1    |   1         |   somewhere/on/the/server.jpg
    2    |   2         |   another/file.py
    3    |   1         |   second/file/of/user/one.py
    

    The USER_ID column is a foreign key that references to the primary key in the users table. Take a look at the docs of Flask-SQLalchemy.