Commit 575a8019 authored by Andrea Bignamini's avatar Andrea Bignamini

Add README files to all examples and fix minor typo bugs

All the README files for all examples have been updated.
Some minor bugs have been fixed in sqlalchemy_examples.
parent ff59ca49
# Object-Relational Mapping Examples
In this repository are available some easy Object-Relational Mapping (ORM) examples.
For more details, please refer to the _Lecture 6 – Data Model Implementation_ of the _Open Data Management & the Cloud_ course.
# Django Example
Create the anaconda environment for this example
conda create -n insurance django
To activate this environment, use
conda activate insurance
Additional packages are needed, not available in Anaconda but installed with the `pip` command
pip install django-extensions djangorestframework
pip install django-composite-field django-url-filter
pip install django-phonenumber-field phonenumbers
pip install Pillow
To deactivate an active environment, use
conda deactivate
## Create a Django Project
Run the following commands
django-admin startproject insurance
cd insurance
python manage.py startapp insurancedb
which create a project folder, named insurance, with additional files and then an application, named insurancedb, inside the project.
It automatically creates skeleton files needed by a Django project and application.
Copy the `admin.py` and `models.py` files to the `insurancedb` folder
cp ../admin.py insurancedb/
cp ../models.py insurancedb/
and copy the `urls.py` and `views.py` file to the `insurance` folder
cp ../urls.py insurance/
cp ../views.py insurance/
Edit the `insurance/setting.py` file adding in the `INSTALLED_APPS` list
two more elements: `django_extensions` and `insurancedb`. Then your
application definition should appear like this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'insurancedb',
]
Finally, generate a migration and migrate
python manage.py makemigrations
python manage.py migrate
Insert some data
python manage.py shell < ../insert.py
Create an admin user
python manage.py createsuperuser
Run the Django web server
python manage.py runserver
Access to <http://127.0.0.1:8000>.
# ORM Example - Euclid
git clone https://www.ict.inaf.it/gitlab/odmc/orm_example.git
cd orm_example/euclid_example
Create the anaconda environment for this example
Crea l'ambiente di anaconda
conda create -n euclid_example django
To activate this environment, use
conda create -n euclid_example
conda activate euclid_example
pip install django-extensions djangorestframework django-composite-field django-url-filter
python manage.py migrate
Additional packages are needed, not available in Anaconda but installed with the `pip` command
pip install django-extensions djangorestframework
pip install django-composite-field django-url-filter
To deactivate an active environment, use
conda deactivate
python manage.py shell
## Run the Django Project
python manage.py runserver
Apply migrations
add admin password
python manage.py migrate
Create an admin user
## Start from scratch
python manage.py createsuperuser
django-admin startproject euclid_example
cd euclid_example
python manage.py startapp imagedb
You can access the database throught the Django shell
Also in settings.py you have to add the name of the app imagedb
python manage.py shell
In the imagedb/models.py add the data model classes
or running the Django web server
Once we have defined our data model in imagedb/models.py we need Django to create the corresponding DB schema
python manage.py runserver
python manage.py makemigrations
then access to <http://127.0.0.1:8000/admin>.
# SQLAlchmy Examples
Create the anaconda environment for this set of examples
conda create -n orm_sqlalchemy sqlalchemy
## # To activate this environment, use
#
# $ conda activate orm_sqlalchemy
#
# To deactivate an active environment, use
#
# $ conda deactivate
To activate this environment, use
conda activate orm_sqlalchemy
To deactivate an active environment, use
conda deactivate
## Example 1
In this first example we are going to map a single _Car_ table.
Each _Car_ has an integer _id_ as primary key, a _name_ and a _price_.
Enter the `example_1` directory
cd example_1
All the SQLAlchmy components
(engine, declarative base class, mapping and session class)
are configured in the `mapping.py` file.
Create the database
python create.py
the SQLite file `example_1.db` has been created.
Now insert some data
python insert.py
Select all data in the database
python read.py
and perform some queries filtered by car name, by id or by car price
python filter.py
Exit from the example_1 directory
cd ..
## Example 2
In this second example we are going to map two tables, _Author_ and _Book_,
connected with a one-to-many relation.
Each _Author_ has an integer _id_ as primary key and a _name_.
Eack _Book_ has an integer _id_ as primary key, a _title_ and an *author_id* as foreign key.
Enter the `example_2` directory
cd example_2
All the SQLAlchmy components
(engine, declarative base class, mapping, relationship and session class)
are configured in the `mapping.py` file.
Create the database
python create.py
the SQLite file `example_2.db` has been created.
Now insert some data
python insert.py
Perform some queries selecting all books by an author or a book to get its author
python filter.py
Exit from the example_2 directory
cd ..
**WARNING**
In SQLite you have to enable foreign key constraint executing
PRAGMA foreign_keys=ON;
## Example 3
In this third example we are going to map inheritance with three tables, _Client_, _Person_ and _Company_, where a _Client_ can be a _Person_ or a _Company_.
Each _Client_ has an integer _id_ as primary key, an _address_ and a _type_.
Each _Person_ has an integer _id_ as primary foreign key, a _name_ and a _surname_.
Each _Company_ has an integer _id_ as primary foreign key, a _company_name_ and a _industry_.
The _type_ discriminates if the _Client_ is also a _Person_ or a _Company_. The _id_ is unique among _Client_, _Person_ and _Company_.
Enter the `example_3` directory
cd example_3
All the SQLAlchmy components
(engine, declarative base class, mapping, relationship and session class)
are configured in the `mapping.py` file.
Create the database
python create.py
the SQLite file `example_3.db` has been created.
Now insert some data
python insert.py
Perform some queries selecting all clients by address or only clients which are persons by address
python filter.py
Exit from the example_3 directory
cd ..
......@@ -28,7 +28,7 @@ class Book(Base):
id = Column(Integer, primary_key=True)
title = Column(String)
author_id = Column(Integer, ForeignKey("Author.id"))
author_id = Column(Integer, ForeignKey("author.id"))
author = relationship("Author")
......
......@@ -25,11 +25,7 @@ results = session.query(Person).filter(Person.address=='via Flavia')
# Print results
print('Persons in via Flavia are:')
for client in results:
if client.type == 'person':
print(client.id, client.name, client.surname)
elif client.type == 'company':
print(client.id, client.company_name, client.industry)
print(client.id, client.name, client.surname)
# Close session
session.close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment