Python & SQL DJ Database
Tags: python, sql, cli, database
Published: 2024-06-24 08:25:41

For our phase 3 projects at Academy Xi we were tasked with building a CLI which features many-to-many databases using SQLAlchemy. For my project I decided to keep it relevant to my hobbies and create a DJ 'Databass' which tracks Melbourne DJs. You can add their DJ name, the genres and subgenres of those genres they play, their music production status, and what venues they have played at. In addition to this you can update, delete, search and view all of this info. Here is a link to the GitHub Repo: https://github.com/saradomincroft/mlb-djs

## Models:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zhcxixttx61fy58pdq0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pcow0t58gh6onblc252.png)

DJ Model: This is the main model for the database, each DJ has a primary key id, a name and a boolean to define whether or not the DJ is also a music producer or not. In addition to this, using SQLAlchemy's relationship and association proxy, connecting the genres and their subgenres, and the venues to the DJs.

Genre Model: This model represents different genres of music. Each genre can have multiple subgenres and can be associated with multiple DJs through the DjGenre association.

DjGenre Model: The DjGenre model is an association table that links DJs to genres, establishing a many-to-many relationship between them.

Subgenre Model: The Subgenre model represents subgenres, which are linked to a parent genre and can also be associated with multiple DJs.

DjSubgenre Model: Similar to DjGenre, DjSubgenre is an association table that links DJs to subgenres.

Venue Model: The Venue model represents venues where DJs perform. Each venue can have multiple DJs, managed through the DjVenue association.

DjVenue Model: Finally, DjVenue is the association table that links DJs to venues.

## Run.py

This is the main file, the main application loop, managed by the start function, provides a user-friendly menu to perform CRUD operations, enabling users to add, update, delete, and view information. I have included some of the more simple functions in this file, the rest of the functions are in a components folder which enabled me to organise the code more efficiently. The clear() function isn't working as planned, I'm still not sure why and have tried troubleshooting this with no success. I also used the fire library so that the menu and start function could be combined into one. I have also made a separate file with some styling using colorama which I have imported throughout the project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdby1ixa3s97h71pbfcw.png)

Clear function imported (not working properly)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftsl5w927huf71qvxibi.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41nx2m814m115xenq6gv.png)

## Add DJ function

This function uses SQLAlchemy to add a new DJ to the database, handling user input to ensure data accuracy and prevent duplicates. The process begins by prompting the user for the DJ's name, ensuring the name is not blank and doesn't already exist in the database. The function then captures whether the DJ produces music and maps genres to titles which have multiple ways of writing them to avoid duplicates. Users can add multiple genres and subgenres, ensuring each entry is valid and not already in the database. Additionally, the function allows users to enter venues where the DJ has performed, again checking for existing entries. Throughout, the check_quit function enables users to exit the process, and error and success messages guide the user. Finally, the new DJ's information is committed to the database, reflecting all the gathered details.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prwy3kok1asf4b27mh31.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xvdgc0g89d9wp262kl1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lglsbvv1wi4e0hsxovi.png)