Skip to content

Commit 74e43de

Browse files
committed
.
1 parent 4302c53 commit 74e43de

File tree

3 files changed

+29
-146
lines changed

3 files changed

+29
-146
lines changed

golden_readme.md

Lines changed: 0 additions & 135 deletions
This file was deleted.

sqlalchemy_1.6_to_2.0/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# SQLAlchemy 1.6 to 2.0 Migration Example
22

3-
[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-blue)](https://docs.codegen.com/tutorials/sqlalchemy-1.6-to-2.0)
4-
53
This example demonstrates how to use Codegen to automatically migrate SQLAlchemy 1.6 code to the new 2.0-style query interface. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/sqlalchemy-1.6-to-2.0).
64

7-
## What This Example Does
5+
## How the Migration Script Works
86

9-
The migration script handles four key transformations:
7+
The codemod script handles four key transformations:
108

119
1. **Convert Query to Select**
1210
```python
@@ -18,6 +16,7 @@ The migration script handles four key transformations:
1816
select(User).where(User.name == 'john')
1917
).scalars().all()
2018
```
19+
This transformation replaces the legacy Query interface with the new Select-based API, providing better type safety and consistency.
2120

2221
2. **Update Session Execution**
2322
```python
@@ -29,6 +28,7 @@ The migration script handles four key transformations:
2928
users = session.execute(select(User)).scalars().all()
3029
first_user = session.execute(select(User)).scalars().first()
3130
```
31+
Session execution is updated to use the new execute() method, which provides clearer separation between SQL construction and execution.
3232

3333
3. **Modernize ORM Relationships**
3434
```python
@@ -42,6 +42,7 @@ The migration script handles four key transformations:
4242
class Address(Base):
4343
user = relationship("User", back_populates="addresses")
4444
```
45+
Relationships are modernized to use explicit back_populates instead of backref, making bidirectional relationships more maintainable and explicit.
4546

4647
4. **Add Type Annotations**
4748
```python
@@ -59,6 +60,7 @@ The migration script handles four key transformations:
5960
name: Mapped[str] = mapped_column()
6061
addresses: Mapped[List["Address"]] = relationship()
6162
```
63+
Type annotations are added using SQLAlchemy 2.0's Mapped[] syntax, enabling better IDE support and runtime type checking.
6264

6365
## Running the Example
6466

@@ -70,13 +72,7 @@ pip install codegen
7072
python run.py
7173
```
7274

73-
The script will process all Python files in the `repo-before` directory and apply the transformations in the correct order.
74-
75-
## Understanding the Code
76-
77-
- `run.py` - The migration script
78-
- `repo-before/` - Sample SQLAlchemy 1.6 application to migrate
79-
- `guide.md` - Additional notes and explanations
75+
The script will process all Python files in the `input_repo` directory and apply the transformations in the correct order.
8076

8177
## Learn More
8278

sqlalchemy_type_annotations/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ class Book(Base):
127127
)
128128
```
129129

130+
## Key Differences to Note
131+
132+
1. **Import Changes**
133+
- New imports required: `from typing import List, Optional`
134+
- `Column` import is replaced with `mapped_column`
135+
- New `Mapped` type wrapper is required
136+
137+
2. **Column Definition Syntax**
138+
- Old: `column_name = Column(type, **kwargs)`
139+
- New: `column_name: Mapped[type] = mapped_column(**kwargs)`
140+
- Type is moved from constructor to type annotation
141+
142+
3. **Relationship Changes**
143+
- `backref` parameter is deprecated in favor of explicit `back_populates`
144+
- Relationships require type hints with `Mapped[List["Model"]]` or `Mapped[Optional["Model"]]`
145+
- Forward references use string literals for model names
146+
147+
4. **Nullable Handling**
148+
- Nullable fields must use `Optional[type]` in type annotation
149+
- Both the type annotation and `nullable=True` parameter are required
150+
- Example: `field: Mapped[Optional[str]] = mapped_column(nullable=True)`
151+
130152
## Running the Migration
131153

132154
```bash

0 commit comments

Comments
 (0)