Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding MetaData() from SQLAlchemy in Python

Tags:

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.

like image 253
Veliko Avatar asked May 21 '17 15:05

Veliko


People also ask

What is the use of MetaData in SQLAlchemy?

Metadata contains definitions of tables and associated objects such as index, view, triggers, etc. Hence an object of MetaData class from SQLAlchemy Metadata is a collection of Table objects and their associated schema constructs.

What does First () do in SQLAlchemy?

first() applies a limit of one within the generated SQL, so that only one primary entity row is generated on the server side (note this may consist of multiple result rows if join-loaded collections are present).

What is the use of SQLAlchemy in Python?

SQLAlchemy is a library that facilitates the communication between Python programs and databases. Most of the times, this library is used as an Object Relational Mapper (ORM) tool that translates Python classes to tables on relational databases and automatically converts function calls to SQL statements.

What is MetaData in database?

A meta data is the data about the data. It is the self-describing nature of databases. It holds the information about each data element in the database. Such as names, types, range of values, access authorization, indicate which application program uses the data.


1 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'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.

like image 58
Sam Hartman Avatar answered Oct 01 '22 09:10

Sam Hartman