Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Created February 12, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whosaysni/c6ccb77a1e2949ab6e7ade78019f7162 to your computer and use it in GitHub Desktop.
Save whosaysni/c6ccb77a1e2949ab6e7ade78019f7162 to your computer and use it in GitHub Desktop.
Why don't you exploit functools.partial with sqlalchemy.Column
from functools import partial
from sqlalchemy.dialects.mysql import (
BIGINT, SMALLINT, TINYINT, INTEGER, DECIMAL,
VARCHAR, TIMESTAMP, DATETIME)
from sqlalchemy.sql.expression import func
from sqlalchemy import Column, Index, MetaData, Table
auto_pk=dict(autoincrement=True, primary_key=True)
UINT5Column = partial(
Column, type_=SMALLINT(5, unsigned=True))
UINT3Column = partial(
Column, type_=TINYINT(3, unsigned=True))
STR45Column = partial(Column, type_=VARCHAR(45))
ActorId = partial(UINT5Column, name='actor_id')
AddressId = partial(UINT5Column, name='address_id')
CityId = partial(UINT5Column, name='city_id')
CategoryId = partial(UINT3Column, name='category_id')
CountryId = partial(UINT5Column, name='country_id')
CustomerId = partial(UINT5Column, name='customer_id')
StoreId = partial(UINT3Column, name='store_id')
FilmId = partial(UINT5Column, name='film_id')
LastUpdate = partial(
Column, name='last_update', type_=TIMESTAMP,
default=func.now(), onupdate=func.now(),
)
FirstName = partial(STR45Column, name='first_name')
LastName = partial(STR45Column, name='last_name')
actor_table = Table(
'actor', MetaData(),
ActorId(**auto_pk),
FirstName(),
LastName(),
LastUpdate(),
Index('idx_actor_last_name', 'last_name'),
)
address_table = Table(
'address', MetaData(),
AddressId(**auto_pk),
Column('address', VARCHAR(50)),
Column('address2', VARCHAR(50), nullable=True),
Column('district', VARCHAR(20)),
CityId(),
Column('postal_code', VARCHAR(10), nullable=True),
Column('phone', VARCHAR(20)),
LastUpdate(),
Index('idx_fk_city_id', 'city_id'),
# Index('idx_location', 'location'),
)
category_table = Table(
'category', MetaData(),
CategoryId(**auto_pk),
Column('name', VARCHAR(25)),
FirstName(),
LastName(index=True),
LastUpdate(),
)
city_table = Table(
'city', MetaData(),
CityId(**auto_pk),
CountryId(),
LastUpdate(),
Index('idx_fk_country_id', 'country_id')
)
country_table = Table(
'country', MetaData(),
CountryId(**auto_pk),
Column('country', VARCHAR(50)),
LastUpdate(),
)
customer_table = Table(
'customer', MetaData(),
CustomerId(**auto_pk),
StoreId(),
Column('first_name', VARCHAR(45)),
)
film_table = Table(
'film', MetaData(),
FilmId(**auto_pk),
Column('title', VARCHAR(255)),
)
# ... blah blah blah ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment