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.
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:
rue, nullable=False), Column('host_id', Integer(), ForeignKey('slots.id'), table=, nullable=False), Column('active', Boolean(), table=), Col umn('port', Integer(), table=, nullable=False), Column('description', String(length=120), table=), Column('username', String(length=40), tab le=), Column('password', String(length=40), table=), schema=None), 'network_location_associations': Table('network_location_associations', M etaData(bind=None), Column('network_id', Integer(), ForeignKey('networks.id'), table=), Column('location_id', Integer(), ForeignKey('locations. id'), table=), schema=None), 'machines': Table('machines', MetaData(bind=None), Column('id', Integer(), ForeignKey('items.id'), table=
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'sidea 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. Alternitevly, you can make the link when you use the metadata either in create calls or in reflection calls.