-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from Pylons/sqla-2.0
update mapper patterns following sqla orm migration guide
- Loading branch information
Showing
2 changed files
with
16 additions
and
23 deletions.
There are no files selected for viewing
24 changes: 10 additions & 14 deletions
24
{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/sqlalchemy_models/meta.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
from sqlalchemy.orm import declarative_base | ||
from sqlalchemy.schema import MetaData | ||
from sqlalchemy import MetaData | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
# Recommended naming convention used by Alembic, as various different database | ||
# providers will autogenerate vastly different names making migrations more | ||
# difficult. See: https://alembic.sqlalchemy.org/en/latest/naming.html | ||
NAMING_CONVENTION = { | ||
"ix": "ix_%(column_0_label)s", | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
"ck": "ck_%(table_name)s_%(constraint_name)s", | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"pk": "pk_%(table_name)s" | ||
} | ||
|
||
metadata = MetaData(naming_convention=NAMING_CONVENTION) | ||
Base = declarative_base(metadata=metadata) | ||
class Base(DeclarativeBase): | ||
metadata = MetaData(naming_convention={ | ||
"ix": "ix_%(column_0_label)s", | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
"ck": "ck_%(table_name)s_`%(constraint_name)s`", | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"pk": "pk_%(table_name)s" | ||
}) |
15 changes: 6 additions & 9 deletions
15
{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/sqlalchemy_models/mymodel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
from sqlalchemy import ( | ||
Column, | ||
Index, | ||
Integer, | ||
Text, | ||
) | ||
from sqlalchemy import Index, Integer, Text | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
from typing import Optional | ||
|
||
from .meta import Base | ||
|
||
|
||
class MyModel(Base): | ||
__tablename__ = 'models' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(Text) | ||
value = Column(Integer) | ||
id: Mapped[int] = mapped_column(primary_key=True) | ||
name: Mapped[Optional[str]] | ||
value: Mapped[Optional[int]] | ||
|
||
|
||
Index('my_index', MyModel.name, unique=True, mysql_length=255) |