Understanding MetaData() from SQLAlchemy in Python

Question:

I am trying to understand what the MetaData() created object is in essence. It is used when reflecting and creating databases in Python (using SQLAlchemy package).

Consider the following working code:

/ with preloaded Engine(sqlite:///chapter5.sqlite) and metadata = MetaData(): when I call metadata in the console, it returns ‘MetaData(bind=None)’ /

# Import Table, Column, String, and Integer
from sqlalchemy import Table, Column, String, Integer

# Build a census table: census
census = Table('census', metadata,
               Column('state', String(30)),
               Column('sex', String(1)),
               Column('age', Integer()),
               Column('pop2000', Integer()),
               Column('pop2008',Integer()))

# Create the table in the database
metadata.create_all(engine)

Of course by typing type(metadata) I get exactly what type of object metadata is: sqlalchemy.sql.schema.MetaData. In SQLAlchemy documentation it is written

MetaData is a container object that keeps together many different features of a database (or multiple databases) being described.

However, I am confused, because in the code we only create a table that “points” to metadata. After that, when we call the create_all method on metadata (empty by far), pointing to the database (which is pointed by engine).

Probably my question is silly, but:

How does python exactly connect these instances? Probably the declaration of the census table links metadata to the column names in a two-sided way.


Note: The code is from an exercise from datacamp course.

Asked By: Veliko

||

Answers:

I think you asked how does python (SQLAlchemy you presumably mean) connect the table to the metadata and the metadata to the database and engine.

So database tables in SQLAlchemy belong (are linked to) a metadata object. The table adds itself to the metadata; there is a tables property on the metadata object that acts a lot like a list:

>>> len(models.Base.metadata.tables)
    22

The reason you need the metadata object is:

  • To have a single unit of work for creating and dropping related tables

  • To have a place to collect all the results of a reflection operation

  • To sort related tables based on their dependencies so that foreign key constraints can be created in the right order.

So, the metadata object contains SQLAlchemy’s idea of what it thinks a database might look like. It’s typically populated either from reflection or from you creating table objects (possibly through the declarative base extension).

You can directly associate a metadata object with a real database engine by setting the bind parameter in the metadata constructor. Alternatively, you can make the link when you use the metadata either in create calls or in reflection calls.

Answered By: Sam Hartman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.