Database - UB-ES-2021-A4/Booksy GitHub Wiki
In this project we're using PostgreSQL 14 and SQLite3. PostgreSQL is a relational database, so it has limitations like being unable to create lists inside a table. Because of these limitations sometimes we have to think outside the typical programming solutions that we do. SQLite3 is also a relational Database, but it'll only be used for the development of the project.
Without further ado, we'll explain how to add or modify tables with Django.
Before trying to execute the project, remember to use the command python manage.py makemigrations of all the apps of the project. If you don't do it then you'll have problems with missing tables.
In order to add a table we have to create a class that uses a Django.models in the models.py file inside an app. Inside the class that we've done we can declare the columns that the table will use. After adding the new table, or modifying an existing one, we have to do the next commands on our console:
python manage.py makemigrations
And after everything checks all right:
python manage.py migrate
After these steps you should find in the database that you have updated the tables.
It's important to do these commands before using the database as it should never be uploaded to the repository.
After modifying the tables we want to be able to modify or add new items to the tables. In order to do so we can do it via console commands, programming or using the admin page, which is the easiest and most visual way.
To access the admin page we can go to the page http://127.0.0.1:8000/admin/ with the credentials being user 'admin' and password '1234'.
To add the table to the admin page we have to go to the admin.py file and add:
admin.site.register(TABLE_NAME)
After reloading the admin page we should be able to see the updated table.
To do these simple tasks I'm going to use the tool pgAdmin 4, included with PostgreSQL14. You should be able to find a database backup on the resources channel of the Discord server.
To export the database we will have to right click and backup the database that we want. If we're asked for the binary paths we have to go to File > Preferences > Paths and there we will set the path to PostgreSQL\14\bin . (Default C:\Program Files\PostgreSQL\14\bin) Where we have to select a filename we can write the full path to the file location or we can click on the three dots to the side to see the explorer menu. After that, use the role name postgres (as it's our superuser). The other fields don't have to be filled.
To import the database we will right click and create a new database with the name of the database that we are importing and the user as postgres. After creating the database we will right click on it and select the Restore option where we will look for the backup file. Then we set once more the user as postgres and we will have our data base imported.
The actual model we're trying to follow is the next one:
