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

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
Loading
Loading
Loading
Loading

README.md

0 → 100644
+5 −0
Original line number Original line Diff line number Diff line
# 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.
+75 −0
Original line number Original line Diff line number Diff line
# 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>.
+23 −17
Original line number Original line Diff line number Diff line
# ORM Example - Euclid
# ORM Example - Euclid


    git clone https://www.ict.inaf.it/gitlab/odmc/orm_example.git
Create the anaconda environment for this example
    cd orm_example/euclid_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
    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


python manage.py shell
    pip install django-extensions djangorestframework
    pip install django-composite-field django-url-filter


python manage.py runserver
To deactivate an active environment, use

    conda deactivate


add admin password


## Run the Django Project


## Start from scratch
Apply migrations


    django-admin startproject euclid_example
    python manage.py migrate
    cd euclid_example
    python manage.py startapp imagedb


Also in settings.py you have to add the name of the app imagedb
Create an admin user


In the imagedb/models.py add the data model classes
    python manage.py createsuperuser


Once we have defined our data model in imagedb/models.py we need Django to create the corresponding DB schema
You can access the database throught the Django shell

    python manage.py shell

or running the Django web server

    python manage.py runserver


python manage.py makemigrations
then access to <http://127.0.0.1:8000/admin>.
+115 −7
Original line number Original line Diff line number Diff line
# SQLAlchmy Examples


Create the anaconda environment for this set of examples
Create the anaconda environment for this set of examples


    conda create -n orm_sqlalchemy sqlalchemy
    conda create -n orm_sqlalchemy sqlalchemy


## # To activate this environment, use
To activate this environment, use
#
#     $ conda activate orm_sqlalchemy
#
# To deactivate an active environment, use
#
#     $ conda deactivate


    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 ..
+1 −1
Original line number Original line Diff line number Diff line
@@ -28,7 +28,7 @@ class Book(Base):


    id = Column(Integer, primary_key=True)
    id = Column(Integer, primary_key=True)
    title = Column(String)      
    title = Column(String)      
    author_id = Column(Integer, ForeignKey("Author.id"))
    author_id = Column(Integer, ForeignKey("author.id"))


    author = relationship("Author")
    author = relationship("Author")


Loading