Project: YugabyteDB (App Definition and Development / Database)
File: sqlalchemy.md

title: SQLAlchemy ORM headerTitle: Use an ORM linkTitle: Use an ORM description: Python SQLAlchemy ORM support for YugabyteDB menu: v2.18: identifier: sqlalchemy-orm parent: python-drivers weight: 600 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../sqlalchemy/" class="nav-link active"> <i class="icon-postgres" aria-hidden="true"></i> SQLAlchemy ORM </a> </li> <li > <a href="../django/" class="nav-link"> <i class="fa-brands fa-java" aria-hidden="true"></i> Django ORM </a> </li> </ul> is a popular ORM provider for Python applications, and is widely used by Python developers for database access. YugabyteDB provides full support for SQLAlchemy ORM. Learn how to establish a connection to YugabyteDB database and begin basic CRUD operations using the steps in page. The following sections demonstrate how to perform common tasks required for Python application development using the SQLAlchemy ORM. To download and install SQLAlchemy to your project, use the following command. ```shell pip3 install sqlalchemy ``` You can verify the installation as follows: Open the Python prompt by executing the following command: ```sh python3 ``` From the Python prompt, execute the following commands to check the SQLAlchemy version: ```python import sqlalchemy ``` ```python sqlalchemy.version ``` To start with SQLAlchemy, in your project directory, create 4 Python files - `config.py`,`base.py`,`model.py`, and `main.py` `config.py` contains the credentials to connect to your database. Copy the following sample code to the `config.py` file. ```python db_user = 'yugabyte' db_password = 'yugabyte' database = 'yugabyte' db_host = 'localhost' db_port = 5433 ``` Next, declare a mapping. When using the ORM, the configuration process begins with describing the database tables you'll use, and then defining the classes which map to those tables. In modern SQLAlchemy, these two tasks are usually performed together, using a system known as Declarative Extensions. Classes mapped using the Declarative system are defined in terms of a base class which maintains a catalog of classes and tables relative to that base - this is known as the declarative base class. You create the base class using the `declarative_base()` function. Add the following code to the `base.py` file. ```python from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() ``` Now that you have a base, you can define any number of mapped classes in terms of it. Start with a single table called `employees`, to store records for the end-users using your application. A new class called `Employee` maps to this table. In the class, you define details about the table to which you're mapping; primarily the table name, and names and datatypes of the columns. Add the following to the `model.py` file: ```python from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Table, Column, Integer, String, DateTime, ForeignKey from base import Base class Employee(Base): tablename = 'employees' id = Column(Integer, primary_key=True) name = Column(String(255), unique=True, nullable=False) age = Column(Integer) language = Column(String(255)) ``` After the setup is done, you can connect to the database and create a new session. In the `main.py` file, add the following: ```python import config as cfg from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy import create_engine from sqlalchemy import MetaData from model import Employee from base import Base from sqlalchemy import Table, Column, Integer, String, DateTime, ForeignKey engine = createengine('postgresql://{0}:{1}@{2}:{3}/{4}'.format(cfg.dbuser, cfg.dbpassword, cfg.dbhost, cfg.db_port, cfg.database)) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() tag_1 = Employee(name='Bob', age=21, language='Python') tag_2 = Employee(name='John', age=35, language='Java') tag_3 = Employee(name='Ivy', age=27, language='C++') session.addall([tag1, tag2, tag3]) print('Query returned:') for instance in session.query(Employee): print("Name: %s Age: %s Language: %s"%(instance.name, instance.age, instance.language)) session.commit() ``` When you run the `main.py` file, you should get the output similar to the following: ```text Query returned: Name: Bob Age: 21 Language: Python Name: John Age: 35 Language: Java Name: Ivy Age: 27 Language: C++ ``` Build Python applications using