surreal-orm-lite 0.29.2__tar.gz → 0.29.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/CHANGELOG.md +53 -0
  2. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/PKG-INFO +37 -10
  3. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/README.md +36 -9
  4. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/pyproject.toml +1 -1
  5. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/__init__.py +1 -1
  6. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/query_set.py +227 -86
  7. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/subquery.py +2 -6
  8. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/.gitignore +0 -0
  9. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/LICENSE +0 -0
  10. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/Makefile +0 -0
  11. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/_sdk.py +0 -0
  12. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/aggregations.py +0 -0
  13. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/auth.py +0 -0
  14. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/cache.py +0 -0
  15. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/changefeed.py +0 -0
  16. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/concurrency.py +0 -0
  17. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/connection_config.py +0 -0
  18. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/connection_manager.py +0 -0
  19. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/constants.py +0 -0
  20. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/decoding.py +0 -0
  21. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/edge.py +0 -0
  22. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/embedded.py +0 -0
  23. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/encoding.py +0 -0
  24. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/enum.py +0 -0
  25. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/exceptions.py +0 -0
  26. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/functions.py +0 -0
  27. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/geo.py +0 -0
  28. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/live.py +0 -0
  29. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/model_auth.py +0 -0
  30. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/model_base.py +0 -0
  31. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/py.typed +0 -0
  32. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/q.py +0 -0
  33. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/routing.py +0 -0
  34. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/signals.py +0 -0
  35. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/surql_literal.py +0 -0
  36. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/testing/__init__.py +0 -0
  37. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/testing/database.py +0 -0
  38. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/testing/fixtures.py +0 -0
  39. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/time_travel.py +0 -0
  40. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/transaction.py +0 -0
  41. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/types.py +0 -0
  42. {surreal_orm_lite-0.29.2 → surreal_orm_lite-0.29.3}/src/surreal_orm_lite/utils.py +0 -0
@@ -5,6 +5,59 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.29.3] - 2026-10-18
9
+
10
+ `get_or_create()` and `update_or_create()` bind the queryset's `variables()` in their lookup, as
11
+ `filter()` does. No new public API.
12
+
13
+ ### Fixed
14
+
15
+ - **`get_or_create()` and `update_or_create()` bind `variables()` in their lookup** (#252). The
16
+ lookup ran without the queryset's `variables()`, so a `Var` criterion read `NONE` there:
17
+ `variables(n=10).get_or_create(size__gte=Var("n"), …)` compared `size` with `NONE` and could
18
+ match a record the caller did not mean, which `update_or_create()` then updated, and
19
+ `variables(t="mine").get_or_create(title=Var("t"))` did not find the existing record. The lookup
20
+ now binds the queryset's `variables()`, merged with those of its subquery criteria as `filter()`
21
+ merges them (a subquery that binds one of these names to another value raises `ValueError`), on
22
+ SurrealDB 2.x, 3.x and the embedded engine.
23
+ - **An `exact` `Var` criterion naming a variable of `variables()` is written as that variable's
24
+ value on create** (#252), so the next identical call finds the record:
25
+ `variables(t="mine").get_or_create(title=Var("t"))` behaves as `get_or_create(title="mine")`. On
26
+ the record id and on an edge's `in` and `out`, it is compared as the record id its value names,
27
+ as the same criterion given that value is (`variables(a="person:alice")` with
28
+ `get_or_create(in_=Var("a"), out=bob)` finds or relates `person:alice`); a value that names no
29
+ record id there raises `ValueError` before any request. A model instance given to `variables()`
30
+ is bound and written as its record id, as the instance given as the criterion is; an unsaved one
31
+ raises `ValueError` before any request. An `exact` `Var` that names a session parameter
32
+ (`Var("auth")`) or no variable of `variables()` still raises `SurrealDbError` on create.
33
+
34
+ ```python
35
+ from surreal_orm_lite import Var
36
+
37
+ qs = Room.objects().variables(t="mine", n=10)
38
+ room, created = await qs.get_or_create(title=Var("t")) # as get_or_create(title="mine")
39
+ room, created = await qs.update_or_create(size__gte=Var("n"), title=Var("t"), defaults={"floor": 2})
40
+ ```
41
+
42
+ ### Changed
43
+
44
+ - **Refusals of a `"$name"` criterion in `get_or_create()` and `update_or_create()`** (#252): a
45
+ string criterion that is exactly `$` followed by the name of a variable of a subquery criterion's
46
+ `variables()` now raises `SurrealDbError` too, like one naming a variable of the queryset's
47
+ `variables()`. For a variable, the message names `Var('x')` as the criterion to write, under any
48
+ lookup but `like`/`ilike`, which take no `Var` (pass the pattern itself, or use `regex`). For a
49
+ session parameter, it says to pass the actual value under `exact`, and that `Var('auth')` works
50
+ under any other lookup but `like`/`ilike`, which take no `Var` (pass the pattern itself, or use
51
+ `regex`): `members__contains=Var("auth")` is looked up, never written. On the `id` column it names
52
+ the record id `table:⟨$name⟩` the string would be.
53
+
54
+ ### Deprecated
55
+
56
+ - The `"$$"` escape of a string filter value, deprecated since v0.28.1 (#250) and listed there
57
+ under Changed: a string filter value starting with `$$` loses one `$` (`"$$admin"` finds
58
+ `"$admin"`) and emits a `DeprecationWarning`. A string filter value is always data, so write the
59
+ value with one `$` less. An item of an `in` list never had the escape and is unchanged.
60
+
8
61
  ## [0.29.2] - 2026-10-18
9
62
 
10
63
  The text lookups `startswith`, `endswith`, `like`, `ilike`, `match` and `regex` work: each compiles
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: surreal-orm-lite
3
- Version: 0.29.2
3
+ Version: 0.29.3
4
4
  Summary: Lightweight Django-style ORM for SurrealDB using the official Python SDK. Async support with Pydantic validation.
5
5
  Project-URL: Homepage, https://github.com/EulogySnowfall/SurrealDB-ORM-lite
6
6
  Project-URL: Documentation, https://github.com/EulogySnowfall/SurrealDB-ORM-lite
@@ -394,15 +394,39 @@ raises `ValueError`; when it names a variable of `variables()` or one of these p
394
394
  message names the `Var(...)` to write.
395
395
 
396
396
  `get_or_create()` and `update_or_create()` look up their criteria and write the `exact` ones on
397
- create, so there such a string is refused: `update_or_create(owner="$auth", defaults=…)` would find
398
- and write the record whose `owner` is the string `"$auth"`, the same record for every record user,
399
- and `members__contains="$auth"` would look for the string. A criterion that is exactly `$` followed
400
- by the name of a variable of `variables()` or of one of these parameters raises `SurrealDbError`
401
- before any request, whatever its lookup, saying to pass the actual value as the criterion: the
402
- record id, or the value itself. `Var("auth")` finds the signed-in user's record
397
+ create. Their lookup binds `variables()`, as `filter()` does, so a `Var` criterion reads the
398
+ variable there too, and an `exact` one is written as the variable's value on create: the next
399
+ identical call finds the record.
400
+
401
+ ```python
402
+ qs = Room.objects().variables(t="mine", n=10)
403
+ room, created = await qs.get_or_create(title=Var("t")) # as get_or_create(title="mine")
404
+ room, created = await qs.update_or_create(size__gte=Var("n"), title=Var("t"), defaults={"floor": 2})
405
+ ```
406
+
407
+ On the record id and on an edge's `in` and `out`, such a criterion is compared as the record id
408
+ its value names, as the same criterion given that value is: with `variables(a="person:alice")`,
409
+ `get_or_create(in_=Var("a"), out=bob)` finds or relates `person:alice`. A value that names no record
410
+ id there raises `ValueError` before any request. A model instance given to `variables()` is bound
411
+ and written as its record id, as the instance given as the criterion is (`variables(a=alice)`); an
412
+ unsaved one raises `ValueError` before any request. The `variables()` of a subquery criterion share
413
+ the lookup, as in `filter()`: one that binds a name of the queryset's `variables()` to another
414
+ value raises `ValueError`. `Var("auth")` finds the signed-in user's record
403
415
  (`get_or_create(owner=Var("auth"))` returns it) but cannot be written: with no record to return, it
404
- raises `SurrealDbError`. Any other string, such as `"$admin"`, is looked up and written as it is. On
405
- an edge's `in` and `out`, such a criterion raises the `ValueError` above.
416
+ raises `SurrealDbError`, and so does an `exact` `Var` that names no variable of `variables()`.
417
+
418
+ A string criterion is refused where it would be a trap: `update_or_create(owner="$auth", defaults=…)`
419
+ would find and write the record whose `owner` is the string `"$auth"`, the same record for every
420
+ record user, and `members__contains="$auth"` would look for the string. A criterion that is exactly
421
+ `$` followed by the name of one of these parameters or of a variable the lookup binds (of
422
+ `variables()`, the queryset's or a subquery criterion's) raises `SurrealDbError` before any request,
423
+ whatever its lookup. For a variable, the message names `Var("x")` as the criterion to write, under
424
+ any lookup but `like`/`ilike`, which take no `Var` (pass the pattern itself, or use `regex`). For a
425
+ parameter, it says to pass the actual value as an `exact` criterion (the record id, or the value
426
+ itself), and that `Var("auth")` works under any other lookup but `like`/`ilike`, which take no `Var`
427
+ (pass the pattern itself, or use `regex`): `members__contains=Var("auth")` is looked up, never
428
+ written. Any other string, such as `"$admin"`, is looked up and written as it is. On an edge's `in`
429
+ and `out`, such a criterion raises the `ValueError` above.
406
430
 
407
431
  `variables()` refuses these four names with a `ValueError`: SurrealDB sets them for each session
408
432
  and refuses a query variable of that name. To pass your own value, give it another name, or pass
@@ -737,7 +761,10 @@ async with SurrealDBConnectionManager.transaction() as tx:
737
761
 
738
762
  If the lookup criteria match more than one record, both methods raise `SurrealDbError`
739
763
  (the criteria are not unique). Non-`exact` lookups (e.g. `name__contains`) drive the lookup
740
- but are not written to the record. Without a transaction the behaviour is identical on
764
+ but are not written to the record. The lookup binds the queryset's `variables()`, as `filter()`
765
+ does: `User.objects().variables(e="bob@example.com").get_or_create(email=Var("e"))` finds the user
766
+ whose email is the variable's value, and writes that value on create (see
767
+ [String filter values and `Var()`](#string-filter-values-and-var)). Without a transaction the behaviour is identical on
741
768
  SurrealDB 2.6.x and 3.x; under `objects(tx=)` they participate in the transaction on 3.x,
742
769
  while a buffered 2.6.x transaction raises on the lookup (see the behaviour table).
743
770
 
@@ -343,15 +343,39 @@ raises `ValueError`; when it names a variable of `variables()` or one of these p
343
343
  message names the `Var(...)` to write.
344
344
 
345
345
  `get_or_create()` and `update_or_create()` look up their criteria and write the `exact` ones on
346
- create, so there such a string is refused: `update_or_create(owner="$auth", defaults=…)` would find
347
- and write the record whose `owner` is the string `"$auth"`, the same record for every record user,
348
- and `members__contains="$auth"` would look for the string. A criterion that is exactly `$` followed
349
- by the name of a variable of `variables()` or of one of these parameters raises `SurrealDbError`
350
- before any request, whatever its lookup, saying to pass the actual value as the criterion: the
351
- record id, or the value itself. `Var("auth")` finds the signed-in user's record
346
+ create. Their lookup binds `variables()`, as `filter()` does, so a `Var` criterion reads the
347
+ variable there too, and an `exact` one is written as the variable's value on create: the next
348
+ identical call finds the record.
349
+
350
+ ```python
351
+ qs = Room.objects().variables(t="mine", n=10)
352
+ room, created = await qs.get_or_create(title=Var("t")) # as get_or_create(title="mine")
353
+ room, created = await qs.update_or_create(size__gte=Var("n"), title=Var("t"), defaults={"floor": 2})
354
+ ```
355
+
356
+ On the record id and on an edge's `in` and `out`, such a criterion is compared as the record id
357
+ its value names, as the same criterion given that value is: with `variables(a="person:alice")`,
358
+ `get_or_create(in_=Var("a"), out=bob)` finds or relates `person:alice`. A value that names no record
359
+ id there raises `ValueError` before any request. A model instance given to `variables()` is bound
360
+ and written as its record id, as the instance given as the criterion is (`variables(a=alice)`); an
361
+ unsaved one raises `ValueError` before any request. The `variables()` of a subquery criterion share
362
+ the lookup, as in `filter()`: one that binds a name of the queryset's `variables()` to another
363
+ value raises `ValueError`. `Var("auth")` finds the signed-in user's record
352
364
  (`get_or_create(owner=Var("auth"))` returns it) but cannot be written: with no record to return, it
353
- raises `SurrealDbError`. Any other string, such as `"$admin"`, is looked up and written as it is. On
354
- an edge's `in` and `out`, such a criterion raises the `ValueError` above.
365
+ raises `SurrealDbError`, and so does an `exact` `Var` that names no variable of `variables()`.
366
+
367
+ A string criterion is refused where it would be a trap: `update_or_create(owner="$auth", defaults=…)`
368
+ would find and write the record whose `owner` is the string `"$auth"`, the same record for every
369
+ record user, and `members__contains="$auth"` would look for the string. A criterion that is exactly
370
+ `$` followed by the name of one of these parameters or of a variable the lookup binds (of
371
+ `variables()`, the queryset's or a subquery criterion's) raises `SurrealDbError` before any request,
372
+ whatever its lookup. For a variable, the message names `Var("x")` as the criterion to write, under
373
+ any lookup but `like`/`ilike`, which take no `Var` (pass the pattern itself, or use `regex`). For a
374
+ parameter, it says to pass the actual value as an `exact` criterion (the record id, or the value
375
+ itself), and that `Var("auth")` works under any other lookup but `like`/`ilike`, which take no `Var`
376
+ (pass the pattern itself, or use `regex`): `members__contains=Var("auth")` is looked up, never
377
+ written. Any other string, such as `"$admin"`, is looked up and written as it is. On an edge's `in`
378
+ and `out`, such a criterion raises the `ValueError` above.
355
379
 
356
380
  `variables()` refuses these four names with a `ValueError`: SurrealDB sets them for each session
357
381
  and refuses a query variable of that name. To pass your own value, give it another name, or pass
@@ -686,7 +710,10 @@ async with SurrealDBConnectionManager.transaction() as tx:
686
710
 
687
711
  If the lookup criteria match more than one record, both methods raise `SurrealDbError`
688
712
  (the criteria are not unique). Non-`exact` lookups (e.g. `name__contains`) drive the lookup
689
- but are not written to the record. Without a transaction the behaviour is identical on
713
+ but are not written to the record. The lookup binds the queryset's `variables()`, as `filter()`
714
+ does: `User.objects().variables(e="bob@example.com").get_or_create(email=Var("e"))` finds the user
715
+ whose email is the variable's value, and writes that value on create (see
716
+ [String filter values and `Var()`](#string-filter-values-and-var)). Without a transaction the behaviour is identical on
690
717
  SurrealDB 2.6.x and 3.x; under `objects(tx=)` they participate in the transaction on 3.x,
691
718
  while a buffered 2.6.x transaction raises on the lookup (see the behaviour table).
692
719
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "surreal-orm-lite"
3
- version = "0.29.2"
3
+ version = "0.29.3"
4
4
  description = "Lightweight Django-style ORM for SurrealDB using the official Python SDK. Async support with Pydantic validation."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,4 +1,4 @@
1
- __version__ = "0.29.2"
1
+ __version__ = "0.29.3"
2
2
 
3
3
  from ._sdk import BoundExcluded, BoundIncluded
4
4
  from .aggregations import Aggregation, Avg, Count, Max, Min, Sum
@@ -2,7 +2,7 @@ import contextlib
2
2
  import logging
3
3
  import math
4
4
  import numbers
5
- from collections.abc import Awaitable, Callable, Collection, Iterable, Mapping, Sequence
5
+ from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequence
6
6
  from datetime import datetime, timedelta
7
7
  from typing import TYPE_CHECKING, Any, Self, SupportsIndex, cast
8
8
  from uuid import UUID
@@ -34,10 +34,12 @@ from .subquery import SubqueryCompiler
34
34
  from .surql_literal import inline_variables
35
35
  from .types import Duration
36
36
  from .utils import (
37
+ RECORD_ID_COLUMN,
37
38
  VALID_FIELD_PATTERN,
38
39
  VARIABLE_NAME_VALUE,
39
40
  as_record_value,
40
41
  build_filter_condition,
42
+ coerce_endpoint,
41
43
  coerce_record_id,
42
44
  is_select_item,
43
45
  non_negative_int,
@@ -68,6 +70,18 @@ _CACHE_QUERY = (
68
70
  _CACHE_DISABLED = "QueryCache is disabled — .cache() has no effect"
69
71
 
70
72
 
73
+ def _unwritable_variable(field: str, name: str) -> str:
74
+ """The refusal of an ``exact`` ``Var(name)`` criterion the create of ``get_or_create()``/``update_or_create()``
75
+ cannot write (issue #252): a session parameter, or a name no ``variables()`` of the lookup gives."""
76
+ if name in SESSION_PARAMETERS:
77
+ why = f"it reads SurrealDB's ${name} parameter, whose value only the database knows"
78
+ advice = "Pass the value itself as the criterion or in defaults=."
79
+ else:
80
+ why = f"variables() gives no {name}, so only the database knows its value"
81
+ advice = f"Give {name} to variables(), or pass the value itself as the criterion or in defaults=."
82
+ return f"Cannot create a record from Var('{name}') given for '{field}': {why}. {advice}"
83
+
84
+
71
85
  class QuerySet:
72
86
  """
73
87
  A class used to build, execute, and manage queries on a SurrealDB table associated with a specific model.
@@ -809,13 +823,7 @@ class QuerySet:
809
823
 
810
824
  return parts, variables, counter
811
825
 
812
- def _build_where(
813
- self,
814
- *,
815
- as_of: bool = False,
816
- subqueries: SubqueryCompiler | None = None,
817
- variable_names: Collection[str] = (),
818
- ) -> tuple[str, dict[str, Any]]:
826
+ def _build_where(self, *, as_of: bool = False, subqueries: SubqueryCompiler | None = None) -> tuple[str, dict[str, Any]]:
819
827
  """
820
828
  Build a parameterized WHERE clause from filters, Q objects and ``nearby()`` conditions.
821
829
 
@@ -824,27 +832,18 @@ class QuerySet:
824
832
  by *subqueries*, the statement's compiler; without one it raises ``ValueError`` (S8).
825
833
 
826
834
  Once every condition is compiled, a scalar ``str`` filter value that is ``$`` and the name of one of the
827
- statement's ``variables()`` — this queryset's, its subqueries', or *variable_names* — or of SurrealDB's
828
- session parameters (``$auth``…) gives a ``UserWarning`` naming ``Var(...)``
829
- (:func:`utils.warn_on_variable_names`, v0.28.1), unless ``subqueries.warn`` is false. Without *subqueries*
830
- nothing is checked: only a statement has variables.
831
-
832
- Args:
833
- variable_names: names the checks count as variables besides the statement's (the warning, and the hint
834
- of an edge endpoint's ``ValueError``): the ``variables()`` of the queryset whose
835
- ``get_or_create()``/``update_or_create()`` look up with this one. Kept on *subqueries*
836
- (``outer_variable_names``).
835
+ statement's ``variables()`` — this queryset's or its subqueries' — or of SurrealDB's session parameters
836
+ (``$auth``…) gives a ``UserWarning`` naming ``Var(...)`` (:func:`utils.warn_on_variable_names`, v0.28.1),
837
+ unless ``subqueries.warn`` is false. Without *subqueries* nothing is checked: only a statement has variables.
837
838
 
838
839
  Returns:
839
840
  A tuple of (where_clause_string, variables_dict).
840
841
  The where_clause_string includes the ``WHERE`` keyword, or is empty if no filters.
841
842
  """
842
- if subqueries is not None and variable_names:
843
- subqueries.outer_variable_names = frozenset(variable_names)
844
843
  parts, variables, _ = self._where_parts(0, as_of=as_of, subqueries=subqueries)
845
844
  if subqueries is not None:
846
845
  if subqueries.warn:
847
- warn_on_variable_names(subqueries.variable_like, {*subqueries.variables, *subqueries.outer_variable_names})
846
+ warn_on_variable_names(subqueries.variable_like, subqueries.variables)
848
847
  subqueries.variable_like.clear()
849
848
  if not parts:
850
849
  return "", {}
@@ -2017,8 +2016,7 @@ class QuerySet:
2017
2016
 
2018
2017
  # ==================== Upsert / get_or_create ====================
2019
2018
 
2020
- @staticmethod
2021
- def _criteria_payload(criteria: dict[str, Any]) -> dict[str, Any]:
2019
+ def _criteria_payload(self, criteria: Mapping[str, Any]) -> dict[str, Any]:
2022
2020
  """Field=value pairs usable as a WRITE payload, taken from EQUALITY criteria only.
2023
2021
 
2024
2022
  ``criteria`` keys are filter lookups (``field`` or ``field__lookup``). A literal raw
@@ -2032,23 +2030,37 @@ class QuerySet:
2032
2030
  gets created is the row the next lookup finds: a ``str`` is data, ``"$admin"`` included;
2033
2031
  the deprecated ``"$$admin"`` escape is un-escaped to ``"$admin"`` (otherwise it filtered on
2034
2032
  ``"$admin"`` but stored ``"$$admin"``, never converging and creating a duplicate on every
2035
- call), without a second warning (the lookup warns); and a ``Var`` reference is refused
2036
- outright — its value lives on the server, so it cannot be written back (issue #156
2037
- follow-up). A model instance is written as its record id, the value its lookup binds
2038
- (v0.22.0): ``get_or_create(customer=alice)`` stores ``customer = RecordID(...)``. A string
2039
- ``"$auth"`` naming a session parameter or a variable of ``variables()`` never gets here: the
2040
- caller refused it before the lookup (:meth:`_reject_string_references`).
2033
+ call), without a second warning (the lookup warns). A model instance is written as its
2034
+ record id, the value its lookup binds (v0.22.0): ``get_or_create(customer=alice)`` stores
2035
+ ``customer = RecordID(...)``. A string ``"$auth"`` naming a session parameter or a variable
2036
+ the lookup binds never gets here: the caller refused it before the lookup
2037
+ (:meth:`_reject_string_references`).
2038
+
2039
+ A ``Var("t")`` criterion naming a variable the lookup binds (:meth:`_lookup_variables`: this
2040
+ queryset's ``variables()`` and those of its subquery criteria) is written as that variable's value,
2041
+ through :func:`~surreal_orm_lite.utils.as_record_value` — data, never un-escaped (issue #252): the lookup
2042
+ compared the column with that value, so the next identical call finds the record.
2043
+ ``get_or_create(title=Var("t"))`` with ``variables(t="mine")`` writes ``title = "mine"``. Any other
2044
+ ``Var`` — a session parameter (``Var("auth")``), a name no ``variables()`` gives — is refused: its value
2045
+ is only known to the database, so it cannot be written back (issue #156 follow-up).
2046
+
2047
+ Raises:
2048
+ SurrealDbError: an ``exact`` ``Var`` criterion naming no variable the lookup binds.
2049
+ ValueError: an unsaved model instance; and what compiling the criteria raises (:meth:`_lookup_variables`).
2041
2050
  """
2042
2051
  payload: dict[str, Any] = {}
2052
+ variables: dict[str, Any] | None = None
2043
2053
  for key, value in criteria.items():
2044
2054
  field_name, lookup = parse_lookup(key)
2045
2055
  if lookup != "exact":
2046
2056
  continue
2047
2057
  if isinstance(value, Var):
2048
- raise SurrealDbError(
2049
- f"Cannot create a record from the query-variable reference given for '{field_name}': "
2050
- "its value is only known to the database. Pass the literal value in defaults=."
2051
- )
2058
+ if variables is None:
2059
+ variables = self._lookup_variables(criteria)
2060
+ if value.name not in variables:
2061
+ raise SurrealDbError(_unwritable_variable(field_name, value.name))
2062
+ payload[field_name] = as_record_value(variables[value.name])
2063
+ continue
2052
2064
  payload[field_name] = as_record_value(unescape_filter_value(value, warn=False))
2053
2065
  return payload
2054
2066
 
@@ -2080,72 +2092,170 @@ class QuerySet:
2080
2092
  owner is the string ``"$auth"``, the same record for every record user, and ``members__contains="$auth"``
2081
2093
  would look for the string. A criterion whose read would warn
2082
2094
  (:func:`~surreal_orm_lite.utils.warn_on_variable_names`) — a scalar ``str`` that is ``$`` and the name of a
2083
- variable this queryset's ``variables()`` give, or of one of SurrealDB's session parameters — is refused
2084
- instead, before any request, saying to pass the actual value as the criterion; ``Var(...)`` reads the value
2085
- in a filter but cannot be written. Any other ``"$name"`` is data, looked up and written as it is, and so is
2086
- an item of a list lookup. A criterion the lookup refuses with a ``ValueError`` (:meth:`_compares_with_string`)
2087
- is left to it: on an edge endpoint, which holds a record id, the message names the same ``Var(...)``.
2095
+ variable the lookup binds (:meth:`_lookup_variables`: this queryset's ``variables()`` and, since issue #252,
2096
+ those of its subquery criteria, which its read counted already), or of one of SurrealDB's session
2097
+ parameters — is refused instead, before any request. For a variable, the message names ``Var(...)`` as the
2098
+ criterion to pass: the lookup reads the variable, and the create writes its value. For a session parameter
2099
+ it says to pass the actual value under ``exact`` (``Var("auth")`` is looked up but cannot be written), and
2100
+ that ``Var("auth")`` works under any other lookup, which is only looked up. ``like``/``ilike`` take no
2101
+ ``Var`` (their pattern is translated to a regular expression before it is sent): under them, for a variable
2102
+ and a session parameter alike, the message says neither, but to pass the actual pattern as the criterion,
2103
+ or to use ``regex`` with a ``Var`` holding a regular expression. On the ``id`` column the string is the
2104
+ record id ``table:⟨$name⟩``, which the message names. Any other ``"$name"`` is data, looked up and written as
2105
+ it is, and so is an item of a list lookup. A criterion the lookup refuses with a ``ValueError`` (an edge
2106
+ endpoint, which holds a record id; a text lookup on ``id``) raises that error here, as the lookup would: on
2107
+ an edge endpoint its message names the same ``Var(...)``.
2088
2108
 
2089
2109
  Raises:
2090
2110
  SurrealDbError: the first such criterion.
2091
- """
2092
- for key, value in criteria.items():
2093
- match = VARIABLE_NAME_VALUE.fullmatch(value) if isinstance(value, str) else None
2094
- if match is None:
2095
- continue
2096
- name = match.group(1)
2097
- if name in self._variables:
2098
- source, kind = f"the variable {name} given to variables()", "variable"
2111
+ ValueError: what compiling the criteria raises (:meth:`_lookup_variables`).
2112
+ """
2113
+ candidates = [
2114
+ (key, value, match.group(1))
2115
+ for key, value in criteria.items()
2116
+ if isinstance(value, str) and (match := VARIABLE_NAME_VALUE.fullmatch(value)) is not None
2117
+ ]
2118
+ if not candidates:
2119
+ return
2120
+ variables = self._lookup_variables(criteria)
2121
+ for key, value, name in candidates:
2122
+ if name in variables:
2123
+ source, variable = f"the variable {name} given to variables()", True
2099
2124
  elif name in SESSION_PARAMETERS:
2100
- source, kind = f"SurrealDB's ${name} parameter", "parameter"
2125
+ source, variable = f"SurrealDB's ${name} parameter", False
2101
2126
  else:
2102
2127
  continue
2103
- if not self._compares_with_string(key, value):
2104
- continue
2105
- how = "looked up and written" if parse_lookup(key)[1] == "exact" else "looked up"
2128
+ field_name, lookup = parse_lookup(key)
2129
+ written = lookup == "exact"
2130
+ how = "looked up and written" if written else "looked up"
2131
+ if self._column(field_name) == RECORD_ID_COLUMN:
2132
+ what = f"the record id {coerce_record_id(value, self._model_table)}"
2133
+ else:
2134
+ what = f"the string '${name}'"
2135
+ if lookup in ("like", "ilike"):
2136
+ advice = (
2137
+ f"'{lookup}' cannot take Var('{name}'): its pattern is translated to a regular expression before "
2138
+ "it is sent. Pass the actual pattern as the criterion, or use 'regex' with a Var holding a regular "
2139
+ "expression."
2140
+ )
2141
+ elif variable:
2142
+ advice = f"To read the variable, pass Var('{name}') as the criterion" + (
2143
+ ": the create writes the variable's value." if written else "."
2144
+ )
2145
+ elif written:
2146
+ advice = (
2147
+ f"Pass the actual value as the criterion (the record id, or the value itself); Var('{name}') reads "
2148
+ "the parameter in the lookup but cannot be written."
2149
+ )
2150
+ else:
2151
+ advice = (
2152
+ f"To read the parameter, pass Var('{name}') as the criterion: {key}=Var('{name}') is looked up, "
2153
+ "never written."
2154
+ )
2106
2155
  raise SurrealDbError(
2107
- f"Cannot look up or create a record from the string '${name}' given for '{key}': it names "
2108
- f"{source}, but a string criterion is data, {how} as the string '${name}'. Pass the actual value as "
2109
- f"the criterion (the record id, or the value itself); Var('{name}') reads the {kind} in a filter but "
2110
- "cannot be written."
2156
+ f"Cannot look up or create a record from the string '${name}' given for '{key}': it names {source}, "
2157
+ f"but a string criterion is data, {how} as {what}. {advice}"
2111
2158
  )
2112
2159
 
2113
- def _compares_with_string(self, key: str, value: str) -> bool:
2114
- """Whether the lookup of the criterion ``key=value`` compares a column with the string *value*.
2160
+ def _lookup_probe(self, criteria: Mapping[str, Any]) -> "QuerySet":
2161
+ """The queryset the lookup of ``get_or_create()``/``update_or_create()`` reads with: *criteria* as its
2162
+ filters, a copy of this queryset's ``variables()`` (issue #252) and its transaction (``objects(tx=)``); no
2163
+ ``cache()``, no ``as_of()``."""
2164
+ probe = QuerySet(self.model)
2165
+ probe.filter(**criteria)
2166
+ probe._variables = dict(self._variables)
2167
+ probe._tx = self._tx
2168
+ return probe
2169
+
2170
+ def _lookup_variables(self, criteria: Mapping[str, Any]) -> dict[str, Any]:
2171
+ """The variables the lookup of *criteria* binds (issue #252), by name.
2172
+
2173
+ This queryset's ``variables()``, then those of each subquery criterion (nested ones included), merged as
2174
+ in ``filter()``: the statement :meth:`_lookup_matches` sends binds them all. Compiled, never sent, without
2175
+ a warning.
2115
2176
 
2116
- The criterion is compiled as the lookup compiles it (:meth:`_where_parts`), without a warning: ``True`` when
2117
- the read notes *value* among the strings that warn (``SubqueryCompiler.variable_like``); ``False`` when the
2118
- lookup refuses it with a ``ValueError``, which the lookup raises itself.
2177
+ Raises:
2178
+ ValueError, SurrealDbError: what the lookup raises compiling *criteria*, before any request — S7 for a
2179
+ subquery binding one of these names to another value, an edge endpoint that is no record id…
2119
2180
  """
2120
- compiler = SubqueryCompiler(warn=False)
2121
- try:
2122
- QuerySet(self.model).filter(**{key: value})._where_parts(subqueries=compiler)
2123
- except ValueError:
2124
- return False
2125
- return bool(compiler.variable_like)
2181
+ probe = self._lookup_probe(criteria)
2182
+ compiler = probe._subquery_compiler(warn=False)
2183
+ probe._where_parts(subqueries=compiler)
2184
+ return compiler.variables
2185
+
2186
+ def _lookup_criteria(self, criteria: Mapping[str, Any]) -> dict[str, Any]:
2187
+ """*criteria* as the lookup compares them (issue #252).
2188
+
2189
+ An ``exact`` ``Var("k")`` criterion on the record id column or on an edge endpoint (``in``/``out``), naming
2190
+ a variable the lookup binds, is compared with the record id that variable's value names — read as the same
2191
+ criterion given that value is (:func:`~surreal_orm_lite.utils.coerce_record_id`,
2192
+ :func:`~surreal_orm_lite.utils.coerce_endpoint`): ``id=Var("k")`` with ``variables(k="r1")`` compares
2193
+ ``id`` with ``<table>:r1``, the record the create writes. Compared with the variable as it is, a string never
2194
+ equals a record id: the lookup would never find the record it created. Every other criterion is unchanged,
2195
+ a ``Var`` reading its variable as ``filter()`` does.
2196
+
2197
+ Raises:
2198
+ ValueError: the variable's value names no record id there (``in_=Var("a")`` with ``variables(a="x")``,
2199
+ a ``bool`` for ``id``), before any request; and what :meth:`_lookup_variables` raises.
2200
+ """
2201
+ endpoint_columns = self.model._endpoint_columns()
2202
+ looked_up = dict(criteria)
2203
+ variables: dict[str, Any] | None = None
2204
+ for key, value in criteria.items():
2205
+ field_name, lookup = parse_lookup(key)
2206
+ if lookup != "exact" or not isinstance(value, Var):
2207
+ continue
2208
+ column = self._column(field_name)
2209
+ if column != RECORD_ID_COLUMN and column not in endpoint_columns:
2210
+ continue
2211
+ if variables is None:
2212
+ variables = self._lookup_variables(criteria)
2213
+ if value.name not in variables:
2214
+ continue
2215
+ try:
2216
+ record = as_record_value(variables[value.name])
2217
+ if column == RECORD_ID_COLUMN:
2218
+ looked_up[key] = coerce_record_id(record, self._model_table)
2219
+ else:
2220
+ looked_up[key] = coerce_endpoint(record)
2221
+ except ValueError as exc:
2222
+ raise ValueError(
2223
+ f"Var('{value.name}') given for '{key}' is compared as the record id its value names: {exc}"
2224
+ ) from exc
2225
+ return looked_up
2126
2226
 
2127
2227
  async def _lookup_matches(self, criteria: dict[str, Any]) -> list[Any]:
2128
2228
  """Return rows matching ``criteria``, routed through the transaction when attached.
2129
2229
 
2130
2230
  Reuses the parameterized WHERE builder (anti-injection) via a throwaway QuerySet's
2131
- ``filter(**criteria)``, joined to ``self._tx`` (``objects(tx=)``) so the read participates in
2132
- it — seeing the tx's uncommitted writes on an interactive transaction, and raising on a
2231
+ ``filter(**criteria)`` (:meth:`_lookup_probe`), joined to ``self._tx`` (``objects(tx=)``) so the read
2232
+ participates in it — seeing the tx's uncommitted writes on an interactive transaction, and raising on a
2133
2233
  buffered one (consistent with every other read). ``LIMIT 2`` is enough to distinguish
2134
2234
  0 / 1 / >1. A subquery criterion (v0.27.0) is read in the same statement, a block
2135
- ``{ LET $_sq0 = (…); (SELECT … LIMIT 2) };``, run as every read is (:meth:`_run_compiled`).
2136
- The lookup binds no ``variables()``; their names give the hint of an edge endpoint's ``ValueError``
2137
- (``variable_names``). A criterion ``"$name"`` that would warn never gets here
2138
- (:meth:`_reject_string_references`).
2235
+ ``{ LET $_sq0 = (…); (SELECT … LIMIT 2) };``, run as every read is (:meth:`_run_compiled`), never cached.
2236
+
2237
+ The lookup binds this queryset's ``variables()`` (issue #252; until then it bound none, and a ``Var``
2238
+ criterion read ``NONE``), merged with its subquery criteria's as ``filter()`` merges them (S7 when a
2239
+ subquery binds a name to another value), so ``variables(n=10).get_or_create(size__gte=Var("n"))`` compares
2240
+ ``size`` with 10. An ``exact`` ``Var`` criterion on the record id or an edge endpoint is compared with the
2241
+ record id its value names (:meth:`_lookup_criteria`). A criterion ``"$name"`` that would warn never gets
2242
+ here (:meth:`_reject_string_references`).
2243
+
2244
+ Each variable is bound as the value the create writes (:func:`~surreal_orm_lite.utils.as_record_value`): a
2245
+ model instance as its record id, whether a criterion reads it or not — bound as it is, the SDK has no
2246
+ encoder for it. S7 compares the values given to ``variables()``, before that conversion.
2247
+
2248
+ Raises:
2249
+ ValueError: an unsaved model instance in the variables (it has no record id), before any request.
2139
2250
  """
2140
- probe = QuerySet(self.model)
2141
- probe.filter(**criteria)
2142
- probe._tx = self._tx
2251
+ probe = self._lookup_probe(self._lookup_criteria(criteria))
2143
2252
  table = self._model_table
2144
2253
 
2145
2254
  def compile_statement(compiler: SubqueryCompiler) -> tuple[str, dict[str, Any]]:
2146
- where_clause, where_vars = probe._build_where(subqueries=compiler, variable_names=self._variables)
2255
+ where_clause, where_vars = probe._build_where(subqueries=compiler)
2147
2256
  query = f"SELECT * FROM {table}{where_clause} LIMIT 2;"
2148
- return compiler.wrap(query), {**compiler.variables, **where_vars}
2257
+ bound = {name: as_record_value(value) for name, value in compiler.variables.items()}
2258
+ return compiler.wrap(query), {**bound, **where_vars}
2149
2259
 
2150
2260
  rows = await probe._run_compiled(compile_statement)
2151
2261
  return rows if isinstance(rows, list) else []
@@ -2215,14 +2325,29 @@ class QuerySet:
2215
2325
  never written; one the create would have to write — under an ``exact`` lookup, or in
2216
2326
  ``defaults`` — raises ``SurrealDbError`` before any request.
2217
2327
 
2328
+ The lookup binds this queryset's ``variables()``, as ``filter()`` does (issue #252; until then a ``Var``
2329
+ criterion read ``NONE`` there, and a comparison could match, then update, a record the caller did not
2330
+ mean): ``objects().variables(n=10).update_or_create(size__gte=Var("n"), title="mine", defaults=…)``
2331
+ compares ``size`` with 10. A subquery criterion's ``variables()`` share the statement: one binding a name of
2332
+ this queryset's to another value raises S7 (``ValueError``) before any request. An ``exact`` ``Var``
2333
+ criterion naming a variable the lookup binds is written as that variable's value on create
2334
+ (``title=Var("t")`` with ``variables(t="mine")`` writes ``title = "mine"``), so the next identical call
2335
+ finds the record; on the record id and an edge's ``in``/``out`` it is compared as the record id its value
2336
+ names, as the same criterion given that value is. A model instance in ``variables()`` is bound and written as
2337
+ its record id (an unsaved one raises ``ValueError`` before any request). A ``Var`` naming a session
2338
+ parameter (``Var("auth")``) or a name no ``variables()`` gives is looked up but cannot be written:
2339
+ ``SurrealDbError`` before any write.
2340
+
2218
2341
  A ``str`` criterion is data, looked up and written as it is (``owner="$admin"``). A scalar criterion, whatever
2219
- its lookup, that is ``$`` and the name of a variable ``variables()`` gives or of one of SurrealDB's session
2342
+ its lookup, that is ``$`` and the name of a variable the lookup binds or of one of SurrealDB's session
2220
2343
  parameters — ``update_or_create(owner="$auth", …)`` — raises ``SurrealDbError`` before any request
2221
2344
  (v0.28.1): it would find and write the record whose owner is the string ``"$auth"``, the same for every
2222
- record user. The message says to pass the actual value as the criterion (the record id, or the value
2223
- itself); ``Var("auth")`` reads the parameter in a filter but cannot be written (an ``exact`` ``Var``
2224
- criterion raises ``SurrealDbError`` here). On an edge endpoint such a string raises the lookup's
2225
- ``ValueError``.
2345
+ record user. For a variable, the message names ``Var(...)`` as the criterion to pass, under any lookup but
2346
+ ``like``/``ilike``, which take no ``Var`` (pass the pattern itself, or use ``regex``); for a session
2347
+ parameter, it says to pass the actual value (the record id, or the value itself) under ``exact`` and that
2348
+ ``Var("auth")`` works under any other lookup but ``like``/``ilike``, which take no ``Var`` (pass the pattern
2349
+ itself, or use ``regex``): ``members__contains=Var("auth")``. On an edge endpoint such a string raises the
2350
+ lookup's ``ValueError``.
2226
2351
  """
2227
2352
  self._reject_time_travel("update_or_create()")
2228
2353
  self._reject_subquery_writes(criteria, defaults, "update_or_create")
@@ -2272,13 +2397,29 @@ class QuerySet:
2272
2397
  create would have to write — under an ``exact`` lookup, or in ``defaults`` — raises
2273
2398
  ``SurrealDbError`` before any request.
2274
2399
 
2400
+ The lookup binds this queryset's ``variables()``, as ``filter()`` does (issue #252; until then a ``Var``
2401
+ criterion read ``NONE`` there): ``objects().variables(n=10).get_or_create(size__gte=Var("n"), …)`` compares
2402
+ ``size`` with 10. A subquery criterion's ``variables()`` share the statement: one binding a name of this
2403
+ queryset's to another value raises S7 (``ValueError``) before any request. An ``exact`` ``Var`` criterion
2404
+ naming a variable the lookup binds is written as that variable's value on create:
2405
+ ``objects().variables(t="mine").get_or_create(title=Var("t"))`` behaves as ``get_or_create(title="mine")``,
2406
+ and the next identical call returns ``created=False``. On the record id and an edge's ``in``/``out`` it is
2407
+ compared as the record id its value names, as the same criterion given that value is:
2408
+ ``variables(a="person:alice").get_or_create(in_=Var("a"), out=bob)`` finds or relates ``person:alice``. A
2409
+ model instance in ``variables()`` is bound and written as its record id (``variables(a=alice)``); an unsaved
2410
+ one raises ``ValueError`` before any request.
2411
+
2275
2412
  A ``str`` criterion is data, looked up and written as it is (``owner="$admin"``). A scalar criterion, whatever
2276
- its lookup, that is ``$`` and the name of a variable ``variables()`` gives or of one of SurrealDB's session
2413
+ its lookup, that is ``$`` and the name of a variable the lookup binds or of one of SurrealDB's session
2277
2414
  parameters — ``get_or_create(owner="$auth")``, ``get_or_create(members__contains="$auth", …)`` — raises
2278
- ``SurrealDbError`` before any request (v0.28.1), saying to pass the actual value as the criterion (the record
2279
- id, or the value itself). ``get_or_create(owner=Var("auth"))`` returns the signed-in record user's record;
2280
- when there is none it raises ``SurrealDbError``: a ``Var`` cannot be written. On an edge endpoint such a
2281
- string raises the lookup's ``ValueError``.
2415
+ ``SurrealDbError`` before any request (v0.28.1). For a variable, the message names ``Var(...)`` as the
2416
+ criterion to pass, under any lookup but ``like``/``ilike``, which take no ``Var`` (pass the pattern itself,
2417
+ or use ``regex``); for a session parameter, it says to pass the actual value (the record id, or the value
2418
+ itself) under ``exact`` and that ``Var("auth")`` works under any other lookup but ``like``/``ilike``, which
2419
+ take no ``Var`` (pass the pattern itself, or use ``regex``).
2420
+ ``get_or_create(owner=Var("auth"))`` returns the signed-in record user's record; when there is none it raises
2421
+ ``SurrealDbError``: a session parameter's value is only known to the database and cannot be written, nor can
2422
+ a name no ``variables()`` gives. On an edge endpoint such a string raises the lookup's ``ValueError``.
2282
2423
  """
2283
2424
  self._reject_time_travel("get_or_create()")
2284
2425
  self._reject_subquery_writes(criteria, defaults, "get_or_create")
@@ -226,9 +226,6 @@ class SubqueryCompiler:
226
226
  (``"$x"`` gives ``x``), every subquery's included, in order: ``QuerySet._build_where`` warns for those
227
227
  :meth:`names_a_variable` or SurrealDB's session parameters hold once the statement's conditions are
228
228
  compiled (v0.28.1).
229
- outer_variable_names: the ``variables()`` names of the queryset whose ``get_or_create()`` or
230
- ``update_or_create()`` looks up with this statement, which binds none of them: the checks of the filter
231
- values count them as variables (v0.28.1).
232
229
  _stack: the ids of the querysets being rendered, the outer one first (a queryset cannot be its own subquery).
233
230
  """
234
231
 
@@ -243,12 +240,11 @@ class SubqueryCompiler:
243
240
  edge_tables: set[str] = field(default_factory=set)
244
241
  warn: bool = True
245
242
  variable_like: list[str] = field(default_factory=list)
246
- outer_variable_names: frozenset[str] = frozenset()
247
243
  _stack: list[int] = field(default_factory=list)
248
244
 
249
245
  def names_a_variable(self, name: str) -> bool:
250
- """Whether *name* is one of the statement's ``variables()`` so far, or of :attr:`outer_variable_names`."""
251
- return name in self.variables or name in self.outer_variable_names
246
+ """Whether *name* is one of the statement's ``variables()`` so far."""
247
+ return name in self.variables
252
248
 
253
249
  def merge_variables(self, variables: Mapping[str, Any]) -> None:
254
250
  """Add a subquery's ``variables()`` to the statement's, in order.