Skip to content

Commit 96489bd

Browse files
authored
Update SQlAlchemy 1.6 to 2.0 with Type Annotations (#10)
added type annotations
1 parent 0f7a1e9 commit 96489bd

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

sqlalchemy_1.6_to_2.0/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This example demonstrates how to use Codegen to automatically migrate SQLAlchemy
66

77
## What This Example Does
88

9-
The migration script handles three key transformations:
9+
The migration script handles four key transformations:
1010

1111
1. **Convert Query to Select**
1212
```python
@@ -43,6 +43,23 @@ The migration script handles three key transformations:
4343
user = relationship("User", back_populates="addresses")
4444
```
4545

46+
4. **Add Type Annotations**
47+
```python
48+
# From:
49+
class User(Base):
50+
__tablename__ = "users"
51+
id = Column(Integer, primary_key=True)
52+
name = Column(String)
53+
addresses = relationship("Address")
54+
55+
# To:
56+
class User(Base):
57+
__tablename__ = "users"
58+
id: Mapped[int] = mapped_column(primary_key=True)
59+
name: Mapped[str] = mapped_column()
60+
addresses: Mapped[List["Address"]] = relationship()
61+
```
62+
4663
## Running the Example
4764

4865
```bash

sqlalchemy_1.6_to_2.0/guide.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ The migration focuses on these key updates:
2424
SQLAlchemy 2.0 introduces a new `lazy` parameter for relationship definitions. Update your relationships to use the new `lazy` parameter for improved performance.
2525
[Run the Relationship Lazy Loading Codemod](https://www.codegen.sh/search/6512?skillType=codemod)
2626

27+
5. **Type Annotations**
28+
SQLAlchemy 2.0 has improved type annotation support. Update your models to include type hints for better IDE support and runtime type checking.
29+
30+
- Add type annotations to model attributes and relationships
31+
- Leverage SQLAlchemy's typing module for proper type hints
32+
- Enable better IDE autocompletion and type checking
33+
34+
[Run the Type Annotations Codemod](https://www.codegen.sh/search/4645?skillType=codemod)
35+
2736
---
2837

2938
## How to Migrate
@@ -69,6 +78,17 @@ SQLAlchemy 2.0 introduces a new `lazy` parameter for relationship definitions. U
6978

7079
---
7180

81+
### Step 5: Add Type Annotations
82+
SQLAlchemy 2.0 has improved type annotation support. Update your models to include type hints for better IDE support and runtime type checking.
83+
84+
- Add type annotations to model attributes and relationships
85+
- Leverage SQLAlchemy's typing module for proper type hints
86+
- Enable better IDE autocompletion and type checking
87+
88+
👉 [Run the Type Annotations Codemod](https://www.codegen.sh/search/4645?skillType=codemod)
89+
90+
---
91+
7292
## Need Help?
7393

7494
If you encounter issues or have specific edge cases not addressed by the codemods, reach out to the Codegen support team or visit the [Codegen Documentation](https://www.codegen.sh/docs) for detailed guidance.
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1+
from typing import List, Optional
12
from sqlalchemy import Column, Integer, String, ForeignKey
2-
from sqlalchemy.orm import relationship, backref
3+
from sqlalchemy.orm import relationship, Mapped, mapped_column
34
from database import Base
45

56
class Publisher(Base):
67
__tablename__ = "publishers"
78

8-
id = Column(Integer, primary_key=True, index=True)
9-
name = Column(String, unique=True, index=True)
10-
books = relationship("Book", back_populates="publisher",lazy='selectin', lazy='selectin')
9+
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
10+
name: Mapped[str] = mapped_column(String, unique=True, index=True)
11+
books: Mapped[List["Book"]] = relationship(
12+
"Book",
13+
back_populates="publisher",
14+
lazy='selectin'
15+
)
1116

1217
class Book(Base):
1318
__tablename__ = "books"
1419

15-
id = Column(Integer, primary_key=True, index=True)
16-
title = Column(String, index=True)
17-
author = Column(String, index=True)
18-
description = Column(String)
19-
publisher_id = Column(Integer, ForeignKey("publishers.id"))
20+
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
21+
title: Mapped[str] = mapped_column(String, index=True)
22+
author: Mapped[str] = mapped_column(String, index=True)
23+
description: Mapped[Optional[str]] = mapped_column(String, nullable=True)
24+
publisher_id: Mapped[Optional[int]] = mapped_column(
25+
Integer,
26+
ForeignKey("publishers.id"),
27+
nullable=True
28+
)
29+
publisher: Mapped[Optional["Publisher"]] = relationship(
30+
"Publisher",
31+
back_populates="books"
32+
)

0 commit comments

Comments
 (0)