diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a1b33682a0c3dd4551a2c4ea421280fff9a2b53e --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# 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. diff --git a/django_example/README.md b/django_example/README.md new file mode 100644 index 0000000000000000000000000000000000000000..790e976f256d31eed7718fe409c7c91f992f959a --- /dev/null +++ b/django_example/README.md @@ -0,0 +1,75 @@ +# 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>. diff --git a/euclid_example/README.md b/euclid_example/README.md index ec2f30294bfc943b56383001cfc89a7e50b8f6ac..256a0db9a1b1257772e915179c3244ebfafd2ee8 100644 --- a/euclid_example/README.md +++ b/euclid_example/README.md @@ -1,33 +1,39 @@ # 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>. diff --git a/sqlalchemy_example/README.md b/sqlalchemy_example/README.md index 0fb1f1e09208b53f90c6201cf91810ca248c4e04..fff7165b1953ab6c14c44614bf8254460f834d91 100644 --- a/sqlalchemy_example/README.md +++ b/sqlalchemy_example/README.md @@ -1,14 +1,122 @@ +# 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 .. diff --git a/sqlalchemy_example/example_2/mapping.py b/sqlalchemy_example/example_2/mapping.py index ab9f931dc18460c289cfe1bf3b884fe79ccf2c3b..fc869f8b7ef29254370b890647a659c6b736c658 100644 --- a/sqlalchemy_example/example_2/mapping.py +++ b/sqlalchemy_example/example_2/mapping.py @@ -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") diff --git a/sqlalchemy_example/example_3/filter.py b/sqlalchemy_example/example_3/filter.py index b75bc34593c0e15c198f95ec7df46b56fc6f638b..caefab910d2bd92db429e9ddb8edb705abf0163f 100644 --- a/sqlalchemy_example/example_3/filter.py +++ b/sqlalchemy_example/example_3/filter.py @@ -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()