Google Cloud Spanner Dialect for SQLAlchemyGoogle Cloud Spanner Dialect for SQLAlchemySoftware Engineer

 from sqlalchemy import (
 Column,
 Computed,
 ForeignKey,
 Integer,
 MetaData,
 String,
 Table,
)

metadata = MetaData(bind=engine)

singers = Table(
 "Singers",
 metadata,
 Column("SingerId", String(36), primary_key=True, nullable=False),
 Column("FirstName", String(200)),
 Column("LastName", String(200), nullable=False),
 Column("FullName", String(400), Computed("COALESCE(FirstName || ' ', '') || LastName")),
)

albums = Table(
 "Albums",
 metadata,
 Column("AlbumId", String(36), primary_key=True, nullable=False),
 Column("Title", String(100), nullable=False),
 Column("SingerId", String(36), ForeignKey("Singers.SingerId", name="FK_Albums_Singers"), nullable=False),
)

tracks = Table(
 "Tracks",
 metadata,
 Column("AlbumId", String(36), primary_key=True, nullable=False),
 Column("TrackId", Integer, primary_key=True, nullable=False),
 Column("Title", String(200), nullable=False),
 spanner_interleave_in="Albums",
 spanner_interleave_on_delete_cascade=True,
)
tracks.add_is_dependent_on(albums)

metadata.create_all(engine)

Leave a Comment