- Hack-a-thon code: https://www.github.com/rydevops/sqlalchemyhat
- SQLite browser: https://sqlitebrowser.org/
- What is SQL Alchemy and why should we use it?
- What is ORM (Object Relational Mapper)
- ORM vs SQL
- Establishing a connection to a database engine
- Understanding lazy sessions
- SQLite (in-memory)
- SQLite (to file)
- Postgres
- Working domain models
- Creating a model (table mapper)
- Create new records
- Commiting data
- Querying for records
- Deleting records
- Creating relationships
- One-to-One
- One-to-Many
- Many-to-Many (time permitting)
- Interacting with models with relationships
The following hack-a-thon will use an SQL Lite database for demonstration. Additional detail on how to interact with other databases (e.g. postgres) can be found in the documentation.
This demonstration was created using Python 3.6 and requires the following python package:
- sqlalchemy
python3.6 -m venv venv
source venv/bin/activate
python3.6 -m pip install sqlalchemy
Q: Can I use SQL for more advanced scenarios?
A: Yes, SQL Alchemy has a few ways of allowing you to execute SQL if required. For example sqlalchemy.text allows you to specify queries as part of the query.
Q: Can I perform operations such as SQL Joins (querying multiple tables at one time)?
A: Yes. The query function also provides a join function that will pre-populate multiple domain models.
Q: Is SQL Alchemy the only ORM for Python?
A: No. Python offers a few frameworks such a s django which implement their own ORM patterns often looking very similar to SQL Alchemy.
Q: Can I change the model/table through SQL Alchemy (also known as a migration)?
A: The default configuration of SQL Alchemy does not provide a migration facility out of the box however a seperate package (SQL Alchemy Migrate) has been created to enable this functionality. Other ORMs such as django do include the migration facilities.
Timebox: 30 minutes
As a group perform the following actions on a single computer:
- Create a new virtual environment and install sqlalchemy
- Setup a new session and engine
- Create the models for 2 to 3 tables from the sample ERD (ignore foreign keys)
- Insert a couple of rows of data into each table.
- Run your code and validate it works
Timebox: 30 minutes
As a group perform the following actions on a single computer:
- Using the tables from the previous challenge setup the foreign key constraints
- Configure relationship variables between the models
- Delete your db.sqlite3 file
- Create some new records that insert data using the new constraints
- Run your code and validate it works