liveBook · Manning

Column

As explained in The Quick Python Book, Fourth Edition

Column is a SQLAlchemy schema construct describing a column in a relational database table, including its name and data type.

A SQLAlchemy Column defines one field in a relational table schema. It combines a column name with a data type and can include options such as primary_key. section 23.4.1

Define columns as arguments to a Table:

  • Column('id', Integer, primary_key=True) creates an integer identifier column. section 23.4.1
  • Column('name', String) and Column('count', Integer) declare string and integer fields. section 23.4.1
  • Table columns become available for SQL expressions, such as people.c.name == 'Jill' in a query filter. section 23.4.1

Define a people table

people = Table('people', metadata,\n Column('id', Integer, primary_key=True),\n Column('name', String),\n Column('count', Integer))\n\nmetadata.create_all(engine) section 23.4.1

dbPath = 'datafile2.db'
engine = create_engine('sqlite:///%s' % dbPath)         #1
metadata = MetaData()
people  = Table('people', metadata,                     #2
                Column('id', Integer, primary_key=True),
                Column('name', String),
                Column('count', Integer),
               )
Session = sessionmaker(bind=engine)                     #3
session = Session()
metadata.create_all(engine)                             #4

See the complete engine, metadata, table, column, session, and schema-creation setup together. section 23.4.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Specifies names, data types, and a primary-key option, then stops short of implementation internals.
Examples
Uses a people table with id, name, and count columns, then inserts, queries, updates, and deletes rows.
What it assumes
Uses SQLAlchemy imports, a database engine, MetaData, Table, and data-type classes alongside Column.
What you can do after
Build it from scratch by declaring typed fields and creating their table through MetaData and an engine.
How it connects
Connects Table, MetaData, relational databases, SQL, ORM mapping, and Python data types.

Also explained in

Compare how 9 books explain column →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.