The second step is to associate a SQLAlchemy session to this Base class, with the appropriate configuration. pyDatalog will use this session to fetch data from the databases.

from sqlalchemy import create_engine

from sqlalchemy.orm import sessionmaker

# create database in memoryengine = create_engine('sqlite:///:memory:', echo=False)# open a session on a database, then associate it to the Base classSession = sessionmaker(bind=engine)session = Session()Base.session = session

Classes that inherit from Base will now have both pyDatalog and SQLAlchemy capability. There are 2 ways to define the attributes of those classes : explicitly, or by inspecting the existing database tables. The first approach enables the creation of the tables in the relational database.

Different classes in a python program can inherit from different Base classes, each having a session on different databases : pyDatalog queries will join data from the relevant databases, performing multi-database queries effortlessly. SQLAlchemy will issue the SQL statements in the proper dialect for each database. Data changes to instances of the classes can be persisted using SQLAlchemy's transaction facilities. See SQLAlchemy's documentation and tutorialfor more details.

Non-relational databases

Queries on non-relational databases can be implemented using predicate resolvers written in python. They can be mixed with queries on relational databases using conjunctive queries or logic clauses.