Skip to content

Instantly share code, notes, and snippets.

@NicolasPA
Last active December 1, 2021 11:01
Show Gist options
  • Select an option

  • Save NicolasPA/6be1da73fc5b340e188bdd8ae23fa75d to your computer and use it in GitHub Desktop.

Select an option

Save NicolasPA/6be1da73fc5b340e188bdd8ae23fa75d to your computer and use it in GitHub Desktop.
Write to file MSSQL create table and create indexe with SQLAlchemy
from sqlalchemy import Table, Column, Index, Integer, VARCHAR, MetaData, ForeignKey
from sqlalchemy.dialects import mssql
from sqlalchemy.schema import CreateTable, CreateIndex
md = MetaData()
trade = Table(
"trade",
md,
Column("pk_trade", Integer, primary_key=True, autoincrement=True),
Column("fk_contract", Integer, ForeignKey("contract.pk_contract"), index=True),
Column("name", VARCHAR(10))
)
contract = Table(
"contract",
md,
Column("pk_contract", Integer, unique=True),
Index("ix_pk_contract", "pk_contract", mssql_clustered=True),
Column("name", VARCHAR(10))
)
tables = [trade, contract]
with open("pint_index.sql", "w") as f:
for table in tables:
query = CreateTable(table)
f.write(query.compile(dialect=mssql.dialect()).string)
for index in table.indexes:
query = CreateIndex(index)
f.write(query.compile(dialect=mssql.dialect()).string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment