Skip to content

Latest commit

 

History

History
168 lines (123 loc) · 5.95 KB

File metadata and controls

168 lines (123 loc) · 5.95 KB

title: sqlalchemy.orm sessionmaker Example Code category: page slug: sqlalchemy-orm-sessionmaker-examples sortorder: 500031074 toc: False sidebartitle: sqlalchemy.orm sessionmaker meta: Python example code that shows how to use the sessionmaker callable from the sqlalchemy.orm module of the SQLAlchemy project.

sessionmaker is a callable within the sqlalchemy.orm module of the SQLAlchemy project.

ColumnProperty, CompositeProperty, Load, Mapper, Query, RelationshipProperty, Session, SynonymProperty, aliased, attributes, backref, class_mapper, column_property, composite, interfaces, mapper, mapperlib, object_mapper, object_session, query, relationship, session, and strategies are several other callables with code examples from the same sqlalchemy.orm package.

Example 1 from graphene-sqlalchemy

graphene-sqlalchemy (project documentation and PyPI package information) is a SQLAlchemy integration for Graphene, which makes it easier to build GraphQL-based APIs into Python web applications. The package allows you to subclass SQLAlchemy classes and build queries around them with custom code to match the backend queries with the GraphQL-based request queries. The project is provided as open source under the MIT license.

graphene-sqlalchemy / graphene_sqlalchemy / tests / conftest.py

# conftest.py
import pytest
from sqlalchemy import create_engine
~~from sqlalchemy.orm import sessionmaker

import graphene

from ..converter import convert_sqlalchemy_composite
from ..registry import reset_global_registry
from .models import Base, CompositeFullName

test_db_url = 'sqlite://'  # use in-memory database for tests


@pytest.fixture(autouse=True)
def reset_registry():
    reset_global_registry()

    @convert_sqlalchemy_composite.register(CompositeFullName)
    def convert_composite_class(composite, registry):
        return graphene.Field(graphene.Int)


@pytest.yield_fixture(scope="function")
def session_factory():
    engine = create_engine(test_db_url)
    Base.metadata.create_all(engine)

~~    yield sessionmaker(bind=engine)

    engine.dispose()


@pytest.fixture(scope="function")
def session(session_factory):
    return session_factory()



## ... source file continues with no further sessionmaker examples...

Example 2 from sqlalchemy-datatables

sqlalchemy-datatables (PyPI package information) is a helper library that makes it easier to use SQLAlchemy with the jQuery JavaScript DataTables plugin. This library is designed to be web framework agnostic and provides code examples for both Flask and Pyramid.

The project is built and maintained by Michel Nemnom (Pegase745) and is open sourced under the MIT license.

sqlalchemy-datatables / tests / conftest.py

# conftest.py
from __future__ import print_function

import itertools
from datetime import datetime, timedelta

import faker
import pytest
from sqlalchemy import create_engine
~~from sqlalchemy.orm import sessionmaker

from .models import Address, Base, User


def populate(session):
    users = []
    f = faker.Faker(seed=1)
    addresses = [Address(description=d) for d in ['Street', 'Avenue', 'Road']]
    session.add_all(addresses)

    for i, addr in zip(range(0, 50), itertools.cycle(addresses)):
        user = User(
            name=f.name(),
            address=addr,
            birthday=datetime(1970, 1, 2) + timedelta(days=10 * i))
        users.append(user)

    session.add_all(users)
    session.commit()


@pytest.fixture(scope="session")
def engine():
    print("TestCase: Using sqlite database")
    return create_engine('sqlite:///', echo=False)


@pytest.fixture(scope="session")
def session(engine):
~~    sessionmaker_ = sessionmaker(bind=engine)
    session = sessionmaker_()
    Base.metadata.create_all(engine)
    populate(session)

    yield session

    session.close()



## ... source file continues with no further sessionmaker examples...