Commit 244069f
committed
Fix: Correct relationship updates with forward references and test logic
This commit addresses several issues:
1. **Forward Reference Conversion in SQLModel:**
I modified `sqlmodel/main.py` in the `_convert_single_pydantic_to_table_model`
function to correctly resolve and convert Pydantic models to SQLModel
table models when forward references (string type hints) are used in
relationship definitions. This ensures that assigning Pydantic objects
to such relationships correctly populates or updates the SQLModel instances.
2. **Test State Leakage in `tests/conftest.py`:**
I introduced an autouse fixture in `tests/conftest.py` that calls
`SQLModel.metadata.clear()` and `default_registry.dispose()` before each
test. This prevents SQLAlchemy registry state from leaking between tests,
resolving "Table already defined" and "Multiple classes found" errors,
leading to more reliable test runs.
3. **Logic in `tests/test_relationships_update.py`:**
I corrected the test logic in `tests/test_relationships_update.py` to
properly update existing entities. Previously, the test was attempting
to add new instances created via `model_validate`, leading to
`IntegrityError` (UNIQUE constraint failed). The test now fetches
existing entities from the session and updates their attributes before
committing, ensuring the update operations are tested correctly.
As a result of these changes, `tests/test_relationships_update.py` now passes,
and all other tests in the suite also pass successfully, ensuring the
stability of the relationship update functionality.1 parent 1229a44 commit 244069f
File tree
3 files changed
+74
-43
lines changed- sqlmodel
- tests
3 files changed
+74
-43
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1221 | 1221 | | |
1222 | 1222 | | |
1223 | 1223 | | |
1224 | | - | |
| 1224 | + | |
1225 | 1225 | | |
1226 | 1226 | | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
1230 | 1232 | | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
1241 | | - | |
1242 | | - | |
1243 | | - | |
1244 | | - | |
1245 | | - | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
1246 | 1246 | | |
1247 | 1247 | | |
1248 | | - | |
| 1248 | + | |
1249 | 1249 | | |
1250 | 1250 | | |
1251 | | - | |
| 1251 | + | |
| 1252 | + | |
1252 | 1253 | | |
1253 | | - | |
| 1254 | + | |
1254 | 1255 | | |
1255 | | - | |
| 1256 | + | |
1256 | 1257 | | |
1257 | 1258 | | |
1258 | 1259 | | |
1259 | 1260 | | |
1260 | | - | |
1261 | | - | |
| 1261 | + | |
| 1262 | + | |
1262 | 1263 | | |
1263 | 1264 | | |
1264 | 1265 | | |
| |||
1277 | 1278 | | |
1278 | 1279 | | |
1279 | 1280 | | |
1280 | | - | |
1281 | | - | |
| 1281 | + | |
| 1282 | + | |
1282 | 1283 | | |
1283 | 1284 | | |
1284 | 1285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
| 88 | + | |
| 89 | + | |
87 | 90 | | |
| 91 | + | |
88 | 92 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
55 | 60 | | |
56 | 61 | | |
57 | 62 | | |
58 | | - | |
59 | 63 | | |
60 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
61 | 86 | | |
| 87 | + | |
62 | 88 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
0 commit comments