Skip to content

Commit c2680b3

Browse files
committed
Migrate model definition to latest sqlalchemy mapped class
1 parent bd658b9 commit c2680b3

5 files changed

Lines changed: 42 additions & 39 deletions

File tree

env.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@
2121
# for 'autogenerate' support
2222
# from myapp import mymodel
2323
# target_metadata = mymodel.Base.metadata
24-
from src.models.base import Base
2524
from src.models.AuthorModel import AuthorModel
2625
from src.models.BookModel import BookModel
2726
from src.models.UserModel import UserModel
27+
from src.models.base import Base
2828

2929
target_metadata = Base.metadata
3030

31-
#from src.models.Database import LibraryMetadata
32-
#target_metadata = LibraryMetadata
33-
3431
# other values from the config, defined by the needs of env.py,
3532
# can be acquired:
3633
# my_important_option = config.get_main_option("my_important_option")
@@ -41,6 +38,7 @@
4138
config.set_section_option(section, "DB_PASSWORD", urllib.parse.quote_plus(os.environ.get('DB_PASSWORD')).replace("%", "%%"))
4239
config.set_section_option(section, "DB_HOST", json_config["DB_HOST"])
4340
config.set_section_option(section, "DB_DATABASE", json_config["DB_DATABASE"])
41+
4442
def run_migrations_offline() -> None:
4543
"""Run migrations in 'offline' mode.
4644

src/models/AuthorModel.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
import sqlalchemy as sa
2+
import sqlalchemy.orm
13
from datetime import datetime
24
from sqlalchemy import Integer, String, DateTime
35
from marshmallow import fields, Schema
46
from sqlalchemy.sql import func
5-
import sqlalchemy as sa
6-
import sqlalchemy.orm
7-
from sqlalchemy.orm import Mapped, mapped_column
7+
from typing import List
8+
from sqlalchemy.orm import Mapped, mapped_column, relationship
89
from quart import Quart
910
from .base import Base
10-
from .BookModel import BookSchema
11+
from .BookModel import BookModel, BookSchema
12+
from .base import Base
1113
from . import db
12-
class AuthorModel(db.Model):
14+
class AuthorModel(Base):
1315
"""
1416
Author Model
1517
"""
1618
__tablename__ = 'authors'
17-
id = db.Column(db.Integer, primary_key=True)
18-
firstname = db.Column(db.String(128), nullable=False)
19-
lastname = db.Column(db.String(128), nullable=False)
20-
email = db.Column(db.String(255), unique=True, nullable=False, index=True)
21-
phone = db.Column(db.String(15), unique=True, nullable=True, index=True)
22-
created_at = db.Column(db.DateTime(timezone=True))
23-
modified_at = db.Column(db.DateTime(timezone=True))
24-
books = db.relationship("BookModel", backref="authors", lazy=True)
19+
id: Mapped[int] = mapped_column(Integer, primary_key=True)
20+
firstname: Mapped[str] = mapped_column(String(128), nullable=False)
21+
lastname: Mapped[str] = mapped_column(String(128), nullable=False)
22+
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
23+
phone: Mapped[str] = mapped_column(String(15), unique=True, nullable=True, index=True)
24+
created_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
25+
modified_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
26+
books: Mapped[List["BookModel"]] = relationship(back_populates="authors", lazy=True)
2527
# Class constructor
2628
def __init__(self, data):
2729
"""

src/models/BookModel.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from sqlalchemy import Integer, String, DateTime
2+
from sqlalchemy import Integer, String, DateTime, ForeignKey
33
from marshmallow import fields, Schema
44
from sqlalchemy.sql import func
55
import sqlalchemy as sa
@@ -8,18 +8,18 @@
88
from quart import Quart
99
from .base import Base
1010
from . import db
11-
class BookModel(db.Model):
11+
class BookModel(Base):
1212
"""
1313
Book Model
1414
"""
1515
__tablename__ = "books"
16-
id = db.Column(db.Integer, primary_key=True)
17-
title = db.Column(db.String(128), nullable=False)
18-
isbn = db.Column(db.String(255), nullable=False)
19-
page_count = db.Column(db.Integer, nullable=False)
20-
created_at = db.Column(db.DateTime(timezone=True))
21-
modified_at = db.Column(db.DateTime(timezone=True))
22-
author_id = db.Column(db.Integer, db.ForeignKey("authors.id"), nullable=False)
16+
id: Mapped[int] = mapped_column(Integer, primary_key=True)
17+
title: Mapped[str] = mapped_column(String(128), nullable=False)
18+
isbn: Mapped[str] = mapped_column(String(255), nullable=False)
19+
page_count: Mapped[int] = mapped_column(Integer, nullable=False)
20+
created_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
21+
modified_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
22+
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"), nullable=False)
2323
def __init__(self, data):
2424
self.author_id = data.get("author_id")
2525
self.title = data.get("title")

src/models/UserModel.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
from sqlalchemy.orm import Mapped, mapped_column
88
from quart import Quart
99
from src.common.Bcrypt import bcrypt
10+
from .base import Base
1011
from . import db
11-
from .BookModel import BookSchema
12-
class UserModel(db.Model):
12+
from .BookModel import BookModel, BookSchema
13+
class UserModel(Base):
1314
"""
1415
User Model
1516
"""
1617
__tablename__ = 'users'
17-
id = db.Column(db.Integer, primary_key=True)
18-
firstname = db.Column(db.String(128), nullable=False)
19-
lastname = db.Column(db.String(128), nullable=False)
20-
email = db.Column(db.String(255), unique=True, nullable=False, index=True)
21-
phone = db.Column(db.String(15), unique=True, nullable=True, index=True)
22-
password = db.Column(db.String(128), nullable=True)
23-
lastlogin = db.Column(db.DateTime(timezone=True), nullable=True)
24-
created_at = db.Column(db.DateTime(timezone=True))
25-
modified_at = db.Column(db.DateTime(timezone=True))
18+
id: Mapped[int] = mapped_column(Integer, primary_key=True)
19+
firstname: Mapped[str] = mapped_column(String(128), nullable=False)
20+
lastname: Mapped[str] = mapped_column(String(128), nullable=False)
21+
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
22+
phone: Mapped[str] = mapped_column(String(15), unique=True, nullable=True, index=True)
23+
password: Mapped[str] = mapped_column(String(128), nullable=True)
24+
lastlogin: Mapped[DateTime] = mapped_column(DateTime(timezone=True), nullable=True)
25+
created_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
26+
modified_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True))
2627
# Class constructor
2728
def __init__(self, data):
2829
"""

src/models/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from sqlalchemy.ext.declarative import declarative_base
2-
Base = declarative_base()
1+
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
2+
3+
class Base(DeclarativeBase):
4+
pass

0 commit comments

Comments
 (0)