surreal-orm-lite 0.36.2__tar.gz → 0.36.4__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 (50) hide show
  1. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/CHANGELOG.md +100 -0
  2. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/PKG-INFO +22 -3
  3. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/README.md +21 -2
  4. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/pyproject.toml +1 -1
  5. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/__init__.py +1 -1
  6. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/connection_manager.py +19 -4
  7. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_base.py +42 -19
  8. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/search.py +103 -16
  9. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/transaction.py +47 -4
  10. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/vector.py +85 -16
  11. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/.gitignore +0 -0
  12. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/LICENSE +0 -0
  13. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/Makefile +0 -0
  14. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/_sdk.py +0 -0
  15. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/aggregations.py +0 -0
  16. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/auth.py +0 -0
  17. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/cache.py +0 -0
  18. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/changefeed.py +0 -0
  19. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/concurrency.py +0 -0
  20. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/connection_config.py +0 -0
  21. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/constants.py +0 -0
  22. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/decoding.py +0 -0
  23. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/define_parser.py +0 -0
  24. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/edge.py +0 -0
  25. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/embedded.py +0 -0
  26. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/encoding.py +0 -0
  27. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/enum.py +0 -0
  28. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/exceptions.py +0 -0
  29. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/functions.py +0 -0
  30. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/geo.py +0 -0
  31. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/introspection.py +0 -0
  32. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/live.py +0 -0
  33. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_auth.py +0 -0
  34. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_ddl.py +0 -0
  35. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_generator.py +0 -0
  36. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/py.typed +0 -0
  37. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/q.py +0 -0
  38. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/query_set.py +0 -0
  39. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/routing.py +0 -0
  40. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/schema.py +0 -0
  41. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/schema_state.py +0 -0
  42. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/signals.py +0 -0
  43. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/subquery.py +0 -0
  44. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/surql_literal.py +0 -0
  45. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/__init__.py +0 -0
  46. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/database.py +0 -0
  47. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/fixtures.py +0 -0
  48. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/time_travel.py +0 -0
  49. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/types.py +0 -0
  50. {surreal_orm_lite-0.36.2 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/utils.py +0 -0
@@ -5,6 +5,106 @@ 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.36.4] - 2026-10-24
9
+
10
+ Follow-ups to the review of v0.36.2 (#322): an index that `remove_index(tx=…)` removed earlier
11
+ in a transaction no longer blocks `remove_analyzer(tx=…)` or `define_analyzer(tx=…)` in the same
12
+ block, `define_indexes(tx=…)` no longer defines an index with an analyzer the block removed or
13
+ redefined, and the v0.36.2 fix is tested on vector (HNSW) indexes. No new public API.
14
+
15
+ ### Fixed
16
+
17
+ - **`remove_analyzer(tx=…)` and `define_analyzer(tx=…)` no longer count an index the block
18
+ removed** (#323). Both find the indexes that use the analyzer by reading `INFO FOR TABLE`
19
+ outside the transaction, where an index that `remove_index(tx=…)` removed earlier in the block
20
+ still stands. Once a block had removed the last index using an analyzer, `remove_analyzer(tx=…)`
21
+ was refused on SurrealDB 2.x and the embedded engine ("in use by index …"), and
22
+ `define_analyzer(…, overwrite=True, tx=…)` was refused on every engine as a redefinition with
23
+ indexes to rebuild. Both now leave out the indexes the transaction noted as removed: the
24
+ analyzer can be removed, or redefined without `REBUILD INDEX`, in the same block, on SurrealDB
25
+ 2.x and 3.x, over WebSocket and HTTP and on the embedded engine. An index still standing keeps
26
+ the refusal, with the same message: another table's, another index of the same table, one
27
+ removed then defined again in the block, one removed with a raw `tx.add("REMOVE INDEX …")`
28
+ (not tracked), or any index when no `tx=` is given. SurrealDB 3.x already accepted the removal,
29
+ since the server checks it in the transaction. An index queued earlier in a buffered block is
30
+ still not seen.
31
+ - **`define_analyzer(tx=…)` after `remove_analyzer(tx=…)` of the same name defines the analyzer
32
+ again** (#323). It compared the analyzer with committed state, where the removed one still
33
+ stood, so an unchanged definition (or any definition without `overwrite=True`) applied nothing
34
+ and the commit left the database without the analyzer. The removed analyzer now counts as
35
+ missing: it is defined again after its removal, with no index read nor rebuilt.
36
+
37
+ ### Changed
38
+
39
+ - **`define_indexes(tx=…)` refuses an index whose analyzer the block removed or redefined**
40
+ (#323), whether the model declares the analyzer as an `Analyzer` or names it by `str`. Until
41
+ v0.36.3 it read the analyzers from committed state only, so it defined such an index: after
42
+ `remove_analyzer(tx=…)`, the index stood without its analyzer once the block committed, and
43
+ every write to the table was then refused; after `define_analyzer(…, overwrite=True, tx=…)`,
44
+ SurrealDB built the index over the table's records with the committed definition, not the new
45
+ one (on SurrealDB 3.x a search then missed those records, without an error). A removed analyzer
46
+ now counts as missing (an `Analyzer` is refused as one to define in a transaction, a name as one
47
+ that does not exist), and an index whose analyzer the block redefined is refused with a new
48
+ message. Both refusals raise `SurrealDbError` before any DDL and do not fail the block. To
49
+ migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first.
50
+
51
+ ### Notes
52
+
53
+ - **Tests**: the v0.36.2 fix (#312) is now covered on a vector (HNSW) index: `remove_index(tx=…)`
54
+ then `define_indexes(tx=…)` in one block, over WebSocket, HTTP and the embedded engine, on an
55
+ empty table and on one holding a record. The index stands after the commit and serves
56
+ `similar_to()`; on SurrealDB 2.x, a table holding a record fails the commit with the retryable
57
+ `SurrealDbConflictError` and keeps the index, as the README's v0.36.2 row says.
58
+ - **Docs**: README section 36 (Transactions) and the docstrings of `define_indexes()`,
59
+ `remove_index()`, `define_analyzer()` and `remove_analyzer()` describe these cases. No
60
+ behaviour-table row changes: for an index removed with `remove_index(tx=…)`, the analyzer
61
+ checks now give the same result on SurrealDB 2.x and 3.x and on the embedded engine.
62
+ - Server pins unchanged: tested against SurrealDB 3.3.4 and 2.7.0 and on the embedded engine,
63
+ plus 3.1.5 for backward compatibility.
64
+
65
+ ## [0.36.3] - 2026-10-24
66
+
67
+ Follow-up to the review of v0.36.0 (#316): a vector field declared with `| None` keeps its
68
+ `Vector` marker, so the checks v0.36.0 added for vector fields apply to it too. No new public API.
69
+
70
+ ### Fixed
71
+
72
+ - **A vector field written `VectorField[d] | None`, `Optional[VectorField[d]]` or
73
+ `Annotated[list[float], Vector(d)] | None` is read as a vector field** (#318). In these
74
+ spellings the marker sits inside a union member, not in the field's metadata, so since v0.36.0
75
+ `get_vector_info()` returned `None` and `is_vector_field()` `False` for them. The `class`
76
+ statement then skipped the vector-index check: an `Index(..., hnsw=True)` of another dimension
77
+ or storage type than the field was accepted, while `define_table()` still wrote
78
+ `option<array<float, d>>`, so the column and the index disagreed without an error; and
79
+ `similar_to()` took the expected length of the query vector from the index, not the field.
80
+ `get_vector_info()` and `is_vector_field()` now read the field's annotation when its metadata
81
+ holds no marker, as `define_table()` does: these spellings give the same column, the same
82
+ index checks at the `class` statement, the same dimension check in `similar_to()` and the same
83
+ `(dimension, storage type)` as `VectorField[d]`. Vectors held in a container
84
+ (`list[VectorField[d]]`) are still not a vector field.
85
+
86
+ ### Changed
87
+
88
+ - **A field with no single vector dimension is refused at the `class` statement** (#318): a
89
+ `Vector` marker next to a type other than `None` in a union (`VectorField[3] | str`), or two
90
+ different `Vector` markers on one field (`Annotated[VectorField[3], Vector(4)]`). The
91
+ `ValueError` names the model and the field and says to write `VectorField[d]` alone (it is
92
+ already optional) or `Annotated[list[float] | None, Vector(d)]`; the check runs on every model,
93
+ whether or not it declares indexes. Until v0.36.2 such a model was accepted (the first of two
94
+ markers decided the checks, a union with another type was not a vector field).
95
+ `get_vector_info()` and `is_vector_field()` raise the same `ValueError` for such a field or
96
+ annotation. Every spelling that v0.36.2 read as a vector field gives the same result.
97
+
98
+ ### Notes
99
+
100
+ - **Docs**: README section 37 (Vector search) says `VectorField[d]` is already optional and
101
+ needs no `| None`: written with it, the field needs `= None`, since Pydantic ignores a default
102
+ inside a union member (with an `UnsupportedFieldAttributeWarning`) and the field is otherwise
103
+ required. It also says how the union spellings are read.
104
+ - The fix is in the ORM's model checks: the behaviour is the same on SurrealDB 2.x and 3.x and on
105
+ the embedded engine. Server pins unchanged: tested against SurrealDB 3.3.4 and 2.7.0, plus
106
+ 3.1.5 for backward compatibility.
107
+
8
108
  ## [0.36.2] - 2026-10-23
9
109
 
10
110
  Follow-ups to the review of v0.35.3 (#312): an index that `remove_index(tx=…)` removed earlier in
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: surreal-orm-lite
3
- Version: 0.36.2
3
+ Version: 0.36.4
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
@@ -6109,8 +6109,19 @@ print(await NoteV2.objects().search(text="physics").count())
6109
6109
  in the same block, fails the commit with a retryable `SurrealDbConflictError` that a retry meets again; so does a
6110
6110
  block that removes an index of a table that held records and defines it again, where the index stands as before.
6111
6111
  Nothing of such a block is committed. On an empty table both commit, as on 3.x.
6112
- - `remove_analyzer(tx=…)` reads committed state to find the indexes that use the analyzer: an index queued earlier in
6113
- a buffered block is not seen.
6112
+ - `remove_analyzer(tx=…)` (on SurrealDB 2.x and the embedded engine; 3.x checks in the transaction itself) and
6113
+ `define_analyzer(tx=…)` read committed state to find the indexes that use the analyzer. An index `remove_index(tx=…)`
6114
+ removed earlier in the block no longer counts, although committed state still holds it: once the block has removed the
6115
+ last index that uses an analyzer, it can remove the analyzer, or redefine it without `REBUILD INDEX`, on every engine.
6116
+ An index removed then defined again in the block, or removed with a raw `tx.add("REMOVE INDEX …")`, still counts
6117
+ there, and an index queued earlier in a buffered block is not seen.
6118
+ - Once the block has removed or redefined an analyzer, an index that uses it can no longer be defined in the block,
6119
+ whether the model declares the analyzer as an `Analyzer` or names it by `str`: `define_indexes(tx=…)` raises
6120
+ `SurrealDbError` before any DDL. A removed analyzer counts as missing, although committed state still holds it — the
6121
+ index would stand without it after the commit, and every write to the table would be refused. A redefined one would
6122
+ not be followed: SurrealDB would build the index with the committed definition. The refusal does not fail the block.
6123
+ To migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first. `define_analyzer(tx=…)` after
6124
+ `remove_analyzer(tx=…)` of the same name defines the analyzer again, after its removal.
6114
6125
  - A search in an interactive transaction sees the transaction's writes; in a buffered one, a read raises, as every
6115
6126
  read does there.
6116
6127
  - In a transaction, a search on a field without a full-text index raises the same `SurrealDbError`, which the
@@ -6299,6 +6310,14 @@ print(get_vector_info(Document.model_fields["embedding"]), is_vector_field(Passa
6299
6310
  # (3, 'F32') False
6300
6311
  ```
6301
6312
 
6313
+ `VectorField[d]` is already optional, so it needs no `| None`. Written `VectorField[d] | None` or
6314
+ `Optional[VectorField[d]]`, its `None` default sits inside a union member, where Pydantic ignores it with an
6315
+ `UnsupportedFieldAttributeWarning`: write `= None`, or the field is required. The ORM reads these spellings, and
6316
+ `Annotated[list[float], Vector(d)] | None`, like the plain ones: the same column, the same checks of the vector index
6317
+ when the class is defined, the same dimension for `similar_to()`, the same answer from `get_vector_info()`. A union
6318
+ that holds a vector next to a type other than `None`, or two different `Vector` markers, has no single dimension: the
6319
+ class statement refuses it, naming the field.
6320
+
6302
6321
  #### Find the nearest
6303
6322
 
6304
6323
  `similar_to(field, vector, limit=10, *, ef=None, dist=None)` keeps the `limit` records whose `field` vector is nearest
@@ -6058,8 +6058,19 @@ print(await NoteV2.objects().search(text="physics").count())
6058
6058
  in the same block, fails the commit with a retryable `SurrealDbConflictError` that a retry meets again; so does a
6059
6059
  block that removes an index of a table that held records and defines it again, where the index stands as before.
6060
6060
  Nothing of such a block is committed. On an empty table both commit, as on 3.x.
6061
- - `remove_analyzer(tx=…)` reads committed state to find the indexes that use the analyzer: an index queued earlier in
6062
- a buffered block is not seen.
6061
+ - `remove_analyzer(tx=…)` (on SurrealDB 2.x and the embedded engine; 3.x checks in the transaction itself) and
6062
+ `define_analyzer(tx=…)` read committed state to find the indexes that use the analyzer. An index `remove_index(tx=…)`
6063
+ removed earlier in the block no longer counts, although committed state still holds it: once the block has removed the
6064
+ last index that uses an analyzer, it can remove the analyzer, or redefine it without `REBUILD INDEX`, on every engine.
6065
+ An index removed then defined again in the block, or removed with a raw `tx.add("REMOVE INDEX …")`, still counts
6066
+ there, and an index queued earlier in a buffered block is not seen.
6067
+ - Once the block has removed or redefined an analyzer, an index that uses it can no longer be defined in the block,
6068
+ whether the model declares the analyzer as an `Analyzer` or names it by `str`: `define_indexes(tx=…)` raises
6069
+ `SurrealDbError` before any DDL. A removed analyzer counts as missing, although committed state still holds it — the
6070
+ index would stand without it after the commit, and every write to the table would be refused. A redefined one would
6071
+ not be followed: SurrealDB would build the index with the committed definition. The refusal does not fail the block.
6072
+ To migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first. `define_analyzer(tx=…)` after
6073
+ `remove_analyzer(tx=…)` of the same name defines the analyzer again, after its removal.
6063
6074
  - A search in an interactive transaction sees the transaction's writes; in a buffered one, a read raises, as every
6064
6075
  read does there.
6065
6076
  - In a transaction, a search on a field without a full-text index raises the same `SurrealDbError`, which the
@@ -6248,6 +6259,14 @@ print(get_vector_info(Document.model_fields["embedding"]), is_vector_field(Passa
6248
6259
  # (3, 'F32') False
6249
6260
  ```
6250
6261
 
6262
+ `VectorField[d]` is already optional, so it needs no `| None`. Written `VectorField[d] | None` or
6263
+ `Optional[VectorField[d]]`, its `None` default sits inside a union member, where Pydantic ignores it with an
6264
+ `UnsupportedFieldAttributeWarning`: write `= None`, or the field is required. The ORM reads these spellings, and
6265
+ `Annotated[list[float], Vector(d)] | None`, like the plain ones: the same column, the same checks of the vector index
6266
+ when the class is defined, the same dimension for `similar_to()`, the same answer from `get_vector_info()`. A union
6267
+ that holds a vector next to a type other than `None`, or two different `Vector` markers, has no single dimension: the
6268
+ class statement refuses it, naming the field.
6269
+
6251
6270
  #### Find the nearest
6252
6271
 
6253
6272
  `similar_to(field, vector, limit=10, *, ef=None, dist=None)` keeps the `limit` records whose `field` vector is nearest
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "surreal-orm-lite"
3
- version = "0.36.2"
3
+ version = "0.36.4"
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.36.2"
1
+ __version__ = "0.36.4"
2
2
 
3
3
  from ._sdk import BoundExcluded, BoundIncluded
4
4
  from .aggregations import Aggregation, Avg, Count, Max, Min, Sum
@@ -2746,7 +2746,15 @@ class SurrealDBConnectionManager:
2746
2746
 
2747
2747
  With *tx*, a new analyzer — or a redefinition no index uses — joins the transaction. A redefinition with indexes
2748
2748
  to rebuild is refused before any DDL: the servers rebuild an index with the analyzers committed before the
2749
- transaction began, so the rebuild must follow the committed definition.
2749
+ transaction began, so the rebuild must follow the committed definition. An index ``Model.remove_index()``
2750
+ removed earlier with the same *tx* is not to rebuild, although committed state still holds it: once the block
2751
+ has removed every index that uses the analyzer, the redefinition joins the transaction, without
2752
+ ``REBUILD INDEX``. An index removed then defined again in the block (``define_indexes(tx=…)``), or removed with
2753
+ a raw ``tx.add("REMOVE INDEX …")``, which the transaction does not track, still counts. The transaction notes
2754
+ the analyzer it defines or redefines: ``Model.define_indexes(tx=…)`` then refuses, before any DDL, to define in
2755
+ the block an index that uses it — SurrealDB would build it with the committed definition. An analyzer
2756
+ :meth:`remove_analyzer` removed earlier with the same *tx* counts as missing, although committed state still
2757
+ holds it: it is defined again after its removal.
2750
2758
 
2751
2759
  :param analyzer: the ``Analyzer`` to define.
2752
2760
  :param overwrite: ``True`` (default) redefines a changed analyzer (``OVERWRITE``) and rebuilds its indexes;
@@ -2790,9 +2798,16 @@ class SurrealDBConnectionManager:
2790
2798
  removed``. SurrealDB 3.x refuses it itself; on SurrealDB 2.x and the embedded engine, which would remove it and
2791
2799
  then refuse every write to that table, the ORM reads the line (``client.version()``), then ``INFO FOR DB`` and
2792
2800
  ``INFO FOR TABLE`` of every table, and refuses it before any DDL. Remove its indexes first
2793
- (``Model.remove_index()``). In a buffered transaction those reads see the committed state: an index queued
2794
- earlier in the block is not seen. No cached read is dropped: a removal succeeds only when no index uses the
2795
- analyzer.
2801
+ (``Model.remove_index()``). With *tx*, those reads see the committed state: an index ``Model.remove_index()``
2802
+ removed earlier with the same *tx* is left out, although committed state still holds it, so a block that
2803
+ removes the last index using the analyzer and then the analyzer commits, as on SurrealDB 3.x, which checks in
2804
+ the transaction; an index removed then defined again in the block, or removed with a raw
2805
+ ``tx.add("REMOVE INDEX …")``, which the transaction does not track, still counts, and an index queued earlier in
2806
+ a buffered block is not seen. The transaction notes the removal: ``Model.define_indexes(tx=…)`` then counts the
2807
+ analyzer as missing, although committed state still holds it, and refuses, before any DDL, to define in the
2808
+ block an index that uses it — the commit would leave that index without its analyzer, and every write to its
2809
+ table refused; :meth:`define_analyzer` with the same *tx* defines it again. No cached read is dropped: a
2810
+ removal succeeds only when no index uses the analyzer.
2796
2811
 
2797
2812
  :param analyzer: the analyzer's name (letters, digits, underscores; not starting with a digit).
2798
2813
  :param missing_ok: ``True`` (default) emits ``REMOVE ANALYZER IF EXISTS …``, so a missing analyzer is not an
@@ -62,8 +62,10 @@ from .schema import (
62
62
  from .schema_state import FieldState, TableState
63
63
  from .search import (
64
64
  Index,
65
+ analyzers_after_removals,
65
66
  apply_analyzers,
66
67
  apply_rebuilds,
68
+ check_analyzers_redefined_in,
67
69
  check_indexes_config,
68
70
  check_named_analyzers,
69
71
  checked_overwrite,
@@ -114,7 +116,7 @@ from .utils import (
114
116
  validate_thing,
115
117
  warn_on_multiple_statements,
116
118
  )
117
- from .vector import get_vector_info
119
+ from .vector import declared_vectors
118
120
 
119
121
  if TYPE_CHECKING:
120
122
  from .edge import BaseSurrealEdge
@@ -877,12 +879,16 @@ class BaseSurrealModel(BaseModel):
877
879
  names — an inherited value is checked again, the messages naming the subclass. So is the
878
880
  ``view_query`` key (v0.32.0): ``None``, or one ``SELECT`` (``schema.check_view_query()``)
879
881
  on a model that is not an edge, does not mix in ``AuthenticatedUserMixin`` and declares no
880
- computed field (``schema.check_view_config()``) — inherited values included. So is, right
881
- after it, the ``indexes`` key (v0.35.0, ``search.check_indexes_config()``): ``None``, or a
882
+ computed field (``schema.check_view_config()``) — inherited values included. So are, right
883
+ after it and on every model, the ``Vector`` markers of the fields (``vector.declared_vectors()``):
884
+ a field holding two different markers, or a marker next to a type other than ``None`` in a
885
+ union, is refused, naming it — inherited fields included, the message naming the subclass. So
886
+ is then the ``indexes`` key (v0.35.0, ``search.check_indexes_config()``): ``None``, or a
882
887
  ``list`` or ``tuple`` of ``Index`` with distinct names, none on a view, one full-text index
883
888
  per column and one ``Analyzer`` definition per analyzer name, one vector index per column
884
- and, on a vector field, of its dimension and storage type (v0.36.0) — inherited values
885
- included, the messages naming the subclass. So are the ``table_type``, ``relation_in``,
889
+ and, on a vector field of any spelling — a union with ``None`` included —, of its dimension
890
+ and storage type (v0.36.0) — inherited values included, the messages naming the subclass.
891
+ So are the ``table_type``, ``relation_in``,
886
892
  ``relation_out`` and ``enforced`` keys (v0.33.0), when any of them is not ``None``: a
887
893
  ``TableType`` (``RELATION`` on an edge only), and the relation keys on an edge model only
888
894
  (``schema.check_relation_config()``). So are, on every model and after the relation keys,
@@ -915,13 +921,11 @@ class BaseSurrealModel(BaseModel):
915
921
  user=issubclass(cls, AuthenticatedUserMixin),
916
922
  computed=tuple(cls.__surreal_computed__),
917
923
  )
924
+ # Every model, with or without indexes: a field whose Vector markers are ambiguous is refused here.
925
+ declared = declared_vectors(cls.__name__, cls.model_fields)
918
926
  indexes = cls.model_config.get("indexes")
919
927
  if indexes is not None:
920
- vectors = {
921
- cls.to_db_field(name): info
922
- for name, field in cls.model_fields.items()
923
- if (info := get_vector_info(field)) is not None
924
- }
928
+ vectors = {cls.to_db_field(name): info for name, info in declared.items()}
925
929
  check_indexes_config(cls, indexes, view=cls._is_view(), column=cls.to_db_field, vectors=vectors)
926
930
  relation = {key: cls.model_config.get(key) for key in ("table_type", "relation_in", "relation_out", "enforced")}
927
931
  if any(value is not None for value in relation.values()):
@@ -1330,6 +1334,14 @@ class BaseSurrealModel(BaseModel):
1330
1334
  ``SurrealDbConflictError`` that a retry meets again: nothing of the block is committed and the index stands as
1331
1335
  before; on an empty table it commits, as on 3.x.
1332
1336
 
1337
+ **An analyzer changed in the transaction** (issue #323). With *tx*, an analyzer
1338
+ ``SurrealDBConnectionManager.remove_analyzer()`` removed with the same *tx* counts as missing, although committed
1339
+ state still holds it: an index using it is refused before any DDL — an ``Analyzer`` object as one to define in a
1340
+ transaction, a name by ``str`` as one that does not exist — instead of standing without it after the commit,
1341
+ which would refuse every write to the table. An index to define (or redefine) whose analyzer
1342
+ ``define_analyzer()`` redefined with the same *tx* is refused too, whether the model declares it or names it:
1343
+ SurrealDB would build it with the committed definition. Call ``define_analyzer()`` without *tx* first.
1344
+
1333
1345
  Names are written in backticks, filters re-rendered from a closed grammar, numbers from Python numbers,
1334
1346
  comments as string literals. A refusal names the analyzer or the index and its table, never the statement:
1335
1347
  ``Can't apply index definition: 'post_title' on table 'post' -> <reason>``. Applying needs a session allowed to
@@ -1340,16 +1352,18 @@ class BaseSurrealModel(BaseModel):
1340
1352
  overwrite: ``True`` (default) redefines a changed analyzer or index — the model wins. ``False`` defines
1341
1353
  what is missing (``IF NOT EXISTS``) and leaves what exists as it is.
1342
1354
  tx: optional transaction the index statements are applied with; the reads happen before anything is
1343
- queued. An index :meth:`remove_index` removed with it counts as missing.
1355
+ queued. An index :meth:`remove_index` removed with it counts as missing, and so does an analyzer
1356
+ ``remove_analyzer()`` removed with it.
1344
1357
 
1345
1358
  Raises:
1346
1359
  TypeError: *overwrite* is not a ``bool`` — before any request.
1347
1360
  SurrealDbError: *tx* belongs to another connection — before any request; an analyzer named by ``str`` does
1348
- not exist, or *tx* is given and an analyzer must be defined or redefined, or an index must be defined
1349
- or redefined (one removed in the block included) after the transaction wrote records to the model's
1350
- table (the message names the model and the table) — before any DDL; the server refused a statement
1351
- (``Can't apply analyzer definition: …``, ``Can't apply index definition: …``, ``Can't apply index
1352
- rebuild: …``).
1361
+ not exist (one removed in the block included), or *tx* is given and an analyzer must be defined or
1362
+ redefined (likewise), or an index must be defined or redefined with an analyzer
1363
+ ``define_analyzer(tx=…)`` redefined in the block, or after the transaction wrote records to the
1364
+ model's table (one removed in the block included; the message names the model and the table) — before
1365
+ any DDL; the server refused a statement (``Can't apply analyzer definition: …``, ``Can't apply index
1366
+ definition: …``, ``Can't apply index rebuild: …``).
1353
1367
  SurrealDbAuthenticationError: the session may not read the definitions or define the analyzers and
1354
1368
  indexes.
1355
1369
 
@@ -1380,6 +1394,9 @@ class BaseSurrealModel(BaseModel):
1380
1394
  # Cheap even inside a transaction: get_client() returns the already-connected client.
1381
1395
  client = await cls._connection_manager().get_client()
1382
1396
  stored_analyzers, tables = await read_database_info(client, what="index", kind="definition", subject=f"table {table!r}")
1397
+ # Issue #323: an analyzer remove_analyzer(tx=…) removed in this transaction still stands in committed state; it is
1398
+ # missing once the transaction commits, so an index using it is refused below — never defined without it.
1399
+ stored_analyzers = analyzers_after_removals(stored_analyzers, tx)
1383
1400
  analyzers = declared_analyzers(indexes)
1384
1401
  check_named_analyzers(indexes, analyzers, stored_analyzers, table=table)
1385
1402
  define, redefine = plan_analyzers(analyzers, stored_analyzers, overwrite)
@@ -1405,6 +1422,10 @@ class BaseSurrealModel(BaseModel):
1405
1422
  keyword=keyword,
1406
1423
  redefined=redefined,
1407
1424
  )
1425
+ analyzer_of = {index.name: index.analyzer_name or "" for index in indexes}
1426
+ # Issue #323: the servers build an index with the analyzers committed before the transaction began, not with
1427
+ # one define_analyzer(tx=…) redefined in it.
1428
+ check_analyzers_redefined_in(tx, cls.__name__, table, definitions, analyzer_of)
1408
1429
  # Issue #304: SurrealDB servers do not index the records a transaction wrote before the index was defined.
1409
1430
  # Under tx the analyzers are in place (refused above otherwise), so the definitions are all that would join it.
1410
1431
  if tx is not None and definitions:
@@ -1413,7 +1434,6 @@ class BaseSurrealModel(BaseModel):
1413
1434
  raise SurrealDbError(transaction_write_refused(cls.__name__, table))
1414
1435
  # A redefined analyzer leaves every index using it with its old tokens: the table's declared indexes (above),
1415
1436
  # its other indexes (another model's, or defined by hand), and every other table's — one INFO FOR TABLE each.
1416
- analyzer_of = {index.name: index.analyzer_name or "" for index in indexes}
1417
1437
  # Issue #304: an index this call redefines is rebuilt by its DEFINE INDEX OVERWRITE; the warning names it too.
1418
1438
  redefined_indexes = [(table, name, analyzer_of[name]) for name, _ in definitions if analyzer_of[name] in redefined]
1419
1439
  own_rebuilds = [(table, name, analyzer_of[name]) for name, _ in rebuilds]
@@ -1453,14 +1473,17 @@ class BaseSurrealModel(BaseModel):
1453
1473
  index as missing, although committed state still holds it, and defines it again after the removal when its
1454
1474
  model declares it — so removing an index and defining it again in one block keeps it. On SurrealDB 2.x, on a
1455
1475
  table that held records, such a block fails the commit with a retryable ``SurrealDbConflictError`` that a retry
1456
- meets again, and nothing of it is committed. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
1476
+ meets again, and nothing of it is committed. ``SurrealDBConnectionManager.remove_analyzer(tx=…)`` and
1477
+ ``define_analyzer(tx=…)`` read the note too: the removed index no longer counts as one using the analyzer (issue
1478
+ #323); once the block has removed or redefined that analyzer, :meth:`define_indexes` refuses to define the index
1479
+ again in it. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
1457
1480
 
1458
1481
  Args:
1459
1482
  name: the index's name.
1460
1483
  missing_ok: ``True`` (default) emits ``REMOVE INDEX IF EXISTS …``, so a missing index is not an error;
1461
1484
  ``False`` raises ``SurrealDbNotFoundError`` for it.
1462
1485
  tx: optional transaction the statement is applied with; it notes the removal for
1463
- :meth:`define_indexes`.
1486
+ :meth:`define_indexes`, ``remove_analyzer()`` and ``define_analyzer()``.
1464
1487
 
1465
1488
  Raises:
1466
1489
  TypeError, ValueError: *name* is not an identifier, or *missing_ok* not a ``bool`` — before any request.
@@ -59,7 +59,11 @@ already read), ``apply_analyzers()`` and ``apply_rebuilds()`` apply the statemen
59
59
  warning per redefined analyzer. ``apply_analyzer()`` and ``apply_analyzer_removal()`` are the bodies of the connection
60
60
  manager's ``define_analyzer()`` and ``remove_analyzer()``; the latter refuses, below SurrealDB 3 (2.x, the embedded
61
61
  engine, which would remove it and then refuse every write to the table), an analyzer an index uses, with 3.x's own
62
- reason.
62
+ reason. With a transaction, both leave out the indexes ``remove_index(tx=…)`` removed earlier in it, which committed
63
+ state still holds (``indexes_removed_in()``, the transaction's note — issue #323), and note the analyzer they removed
64
+ or defined (``note_analyzer_change()``): ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)`` count one the block
65
+ removed as missing (``analyzers_after_removals()``), and ``define_indexes(tx=…)`` refuses an index whose analyzer the
66
+ block redefined (``transaction_redefined_analyzer_refused()``).
63
67
 
64
68
  **The query side.** ``QuerySet.search()`` checks its fields and texts with ``checked_search_field()`` and words its
65
69
  refusals with ``text_refused()``, ``field_refused()``, ``too_many_fields()``, ``time_travel_refused()`` (with
@@ -73,7 +77,8 @@ annotation whose match reference ``search()`` did not give it is refused with ``
73
77
  ``search()`` with ``no_search_refused()``.
74
78
 
75
79
  The module sits low in the package: it imports neither ``model_base``, ``query_set`` nor ``connection_manager``, so
76
- each of them can import it.
80
+ each of them can import it; it imports ``transaction``, which imports none of them, for its note of the removed
81
+ indexes.
77
82
  """
78
83
 
79
84
  import logging
@@ -92,6 +97,7 @@ from .schema import _comment_text, _identifier_name, _missing_clause, _overwrite
92
97
  from .schema_state import AnalyzerState, IndexState
93
98
  from .surql_literal import _identifier, _quoted
94
99
  from .time_travel import line_major
100
+ from .transaction import Transaction
95
101
  from .utils import VALID_FIELD_PATTERN, _ddl_refusal_class, apply_ddl_statements, validate_field_name
96
102
  from .vector import (
97
103
  DEFAULTS,
@@ -833,11 +839,11 @@ def check_indexes_config(
833
839
  by *column* — the model's ``to_db_field`` — so an aliased field counts under its column (SurrealDB would use either
834
840
  for ``search()``, with different scores); two different ``Analyzer`` objects share a name (an analyzer belongs to
835
841
  the database: one definition per name); two vector indexes cover one column, counted the same way (SurrealDB would
836
- use either for ``similar_to()``); a vector index on a vector field — *vectors* maps the column of each, either
837
- spelling, to its ``(dimension, storage type)`` — whose dimension or storage type differs from the field's, the
838
- index's effective type compared (``F32`` when it declares none, ``Index._hnsw_options()``). A vector index on a
839
- column that is not a vector field is not checked against it. Each ``Index`` was checked when it was built. The
840
- messages name *model*.
842
+ use either for ``similar_to()``); a vector index on a vector field — *vectors* maps the column of each, any
843
+ spelling (a union with ``None`` included), to its ``(dimension, storage type)`` — whose dimension or storage type
844
+ differs from the field's, the index's effective type compared (``F32`` when it declares none,
845
+ ``Index._hnsw_options()``). A vector index on a column that is not a vector field is not checked against it. Each
846
+ ``Index`` was checked when it was built. The messages name *model*.
841
847
 
842
848
  :raises TypeError: *value* is not a ``list`` or a ``tuple``, or holds something other than an ``Index``.
843
849
  :raises ValueError: two indexes share a name; a view declares an index; two full-text indexes cover one column;
@@ -1148,25 +1154,61 @@ def stored_indexes_using(
1148
1154
 
1149
1155
 
1150
1156
  async def indexes_using(
1151
- client: Any, tables: Iterable[str], analyzers: Collection[str], *, what: str, kind: str
1157
+ client: Any,
1158
+ tables: Iterable[str],
1159
+ analyzers: Collection[str],
1160
+ *,
1161
+ what: str,
1162
+ kind: str,
1163
+ skip: Callable[[str], Collection[str]] | None = None,
1152
1164
  ) -> list[IndexUse]:
1153
1165
  """The indexes of *tables* whose analyzer is among *analyzers*, as ``(table, index, analyzer)``, sorted by table
1154
1166
  then index — one ``INFO FOR TABLE`` per table, in the order of *tables*, outside any transaction (committed state).
1155
1167
 
1156
1168
  *tables* are names ``INFO FOR DB`` gave (:func:`read_database_info`), each written in backticks, escaped. An index
1157
- the parser cannot read is left out (its analyzer is unknown).
1169
+ the parser cannot read is left out (its analyzer is unknown), and so is one *skip* names for its table: *skip*
1170
+ takes a table's name, as ``INFO FOR DB`` gives it, and returns the names of its indexes to leave out —
1171
+ :func:`indexes_removed_in`'s, the indexes ``remove_index(tx=…)`` removed earlier in a transaction, which committed
1172
+ state still holds (issue #323). Every table is read all the same.
1158
1173
 
1159
1174
  :param what: what the reads are for, for the message of a refusal (``"index"``, ``"analyzer"``).
1160
1175
  :param kind: ``"definition"``, ``"removal"`` or ``"rebuild"``, likewise.
1176
+ :param skip: the indexes to leave out, by table; ``None`` (default) leaves none out.
1161
1177
  :raises SurrealDbAuthenticationError: the session may not read a table's definitions (a record user).
1162
1178
  :raises SurrealDbError: any other refusal of an ``INFO``.
1163
1179
  """
1164
1180
  found: list[IndexUse] = []
1165
1181
  for table in tables:
1166
- found.extend(stored_indexes_using(table, await _table_indexes(client, table, what=what, kind=kind), analyzers))
1182
+ stored = await _table_indexes(client, table, what=what, kind=kind)
1183
+ found.extend(stored_indexes_using(table, stored, analyzers, skip=skip(table) if skip is not None else ()))
1167
1184
  return sorted(found)
1168
1185
 
1169
1186
 
1187
+ def indexes_removed_in(tx: object) -> Callable[[str], frozenset[str]] | None:
1188
+ """The indexes ``remove_index(tx=…)`` removed in *tx* and not defined again since, by table — *tx*'s
1189
+ ``Transaction._indexes_removed`` — for :func:`indexes_using`'s *skip*; ``None`` when *tx* is not a ``Transaction``
1190
+ (no transaction given). A raw ``tx.add("REMOVE INDEX …")`` is not noted (issue #323)."""
1191
+ return tx._indexes_removed if isinstance(tx, Transaction) else None
1192
+
1193
+
1194
+ def analyzers_after_removals(stored: Mapping[str, AnalyzerState | None], tx: object) -> dict[str, AnalyzerState | None]:
1195
+ """The analyzers *stored* holds (``INFO FOR DB``'s, committed state) without those ``remove_analyzer(tx=…)``
1196
+ removed earlier in *tx* and did not define again since — *tx*'s ``Transaction._analyzers_removed`` (issue #323):
1197
+ the analyzers in place once the transaction commits. *stored* as it is when *tx* is not a ``Transaction``. Pure."""
1198
+ removed = tx._analyzers_removed() if isinstance(tx, Transaction) else frozenset()
1199
+ return {name: state for name, state in stored.items() if name not in removed}
1200
+
1201
+
1202
+ def note_analyzer_change(tx: object, name: str, *, removed: bool) -> None:
1203
+ """Note on *tx* that the analyzer *name* was removed (*removed*) or defined in it — once the statement is applied
1204
+ or queued (issue #323); nothing when *tx* is not a ``Transaction``."""
1205
+ if isinstance(tx, Transaction):
1206
+ if removed:
1207
+ tx._note_analyzer_removal(name)
1208
+ else:
1209
+ tx._note_analyzer_definition(name)
1210
+
1211
+
1170
1212
  # --- the appliers' plans --------------------------------------------------------------------------------------------
1171
1213
 
1172
1214
 
@@ -1236,6 +1278,37 @@ def transaction_analyzer_refused(model: str, name: str) -> str:
1236
1278
  )
1237
1279
 
1238
1280
 
1281
+ def transaction_redefined_analyzer_refused(model: str, index: str, table: str, analyzer: str) -> str:
1282
+ """The refusal of ``define_indexes(tx=…)`` when the index *index* of *table* uses the analyzer *analyzer*, which
1283
+ ``define_analyzer(tx=…)`` redefined earlier in the transaction (issue #323): the servers build an index with the
1284
+ analyzers committed before the transaction began."""
1285
+ return (
1286
+ f"{model}.define_indexes(tx=…) cannot define index {index!r} on table {table!r} in a transaction that has "
1287
+ f"redefined its analyzer {analyzer!r}: SurrealDB builds an index with the analyzers committed before the "
1288
+ "transaction began, so the index would not follow the new definition. Call "
1289
+ "SurrealDBConnectionManager.define_analyzer() without tx= first, then define_indexes(tx=…)."
1290
+ )
1291
+
1292
+
1293
+ def check_analyzers_redefined_in(
1294
+ tx: object, model: str, table: str, definitions: Iterable[tuple[str, str]], analyzer_of: Mapping[str, str]
1295
+ ) -> None:
1296
+ """Refuse, before any DDL, the first of *definitions* — ``(index name, statement)``, what ``define_indexes(tx=…)``
1297
+ of the model *model* would define on *table* — whose analyzer (*analyzer_of*, by index name, ``""`` for none)
1298
+ ``define_analyzer(tx=…)`` defined or redefined earlier in *tx* (``Transaction._analyzers_defined``, issue #323):
1299
+ the servers build an index with the analyzers committed before the transaction began. Nothing to refuse when *tx*
1300
+ is not a ``Transaction``.
1301
+
1302
+ :raises SurrealDbError: :func:`transaction_redefined_analyzer_refused`'s text.
1303
+ """
1304
+ if not isinstance(tx, Transaction):
1305
+ return
1306
+ defined = tx._analyzers_defined()
1307
+ for name, _ in definitions:
1308
+ if analyzer_of[name] in defined:
1309
+ raise SurrealDbError(transaction_redefined_analyzer_refused(model, name, table, analyzer_of[name]))
1310
+
1311
+
1239
1312
  def transaction_write_refused(model: str, table: str) -> str:
1240
1313
  """The refusal of ``define_indexes(tx=…)`` when the transaction has already written records to *table*, the
1241
1314
  model's (issue #304): SurrealDB servers leave those records out of the index."""
@@ -1372,20 +1445,29 @@ async def apply_analyzer(client: Any, analyzer: Analyzer, *, overwrite: bool, tx
1372
1445
  (:func:`indexes_using`), then ``DEFINE ANALYZER OVERWRITE`` and ``REBUILD INDEX`` of each index using it — SurrealDB
1373
1446
  leaves the records indexed before with their old tokens — and the warning of :func:`warn_redefined`; nothing
1374
1447
  without *overwrite*. With *tx*, a redefinition with indexes to rebuild is refused before any DDL (the rebuild must
1375
- follow the committed definition); a new analyzer, or a redefinition no index uses, joins the transaction.
1448
+ follow the committed definition); a new analyzer, or a redefinition no index uses, joins the transaction. An index
1449
+ ``remove_index(tx=…)`` removed earlier in *tx* is not to rebuild, although committed state still holds it
1450
+ (:func:`indexes_removed_in`, issue #323): a redefinition whose indexes the block all removed joins the transaction,
1451
+ without ``REBUILD INDEX``. An analyzer ``remove_analyzer(tx=…)`` removed earlier in *tx* counts as missing
1452
+ (:func:`analyzers_after_removals`): defined again after its removal, with no index read nor rebuilt. *tx* notes the
1453
+ analyzer once defined or redefined (:func:`note_analyzer_change`), for ``define_indexes(tx=…)``.
1376
1454
 
1377
1455
  :raises SurrealDbError: *tx* is given and the redefinition has indexes to rebuild; a refused read or statement.
1378
1456
  :raises SurrealDbAuthenticationError: the session may not read the definitions or define the analyzer.
1379
1457
  """
1380
1458
  subject = repr(analyzer.name)
1381
1459
  stored, tables = await read_database_info(client, what="analyzer", kind="definition", subject=subject)
1382
- define, redefine = plan_analyzers([analyzer], stored, overwrite)
1460
+ define, redefine = plan_analyzers([analyzer], analyzers_after_removals(stored, tx), overwrite)
1383
1461
  rebuilds: list[IndexUse] = []
1384
1462
  if redefine:
1385
- rebuilds = await indexes_using(client, tables, {analyzer.name}, what="analyzer", kind="definition")
1463
+ rebuilds = await indexes_using(
1464
+ client, tables, {analyzer.name}, what="analyzer", kind="definition", skip=indexes_removed_in(tx)
1465
+ )
1386
1466
  if tx is not None and rebuilds:
1387
1467
  raise SurrealDbError(analyzer_redefinition_refused(analyzer.name, rebuilds))
1388
1468
  applied = await apply_analyzers(client, tx, define, redefine, overwrite)
1469
+ if applied:
1470
+ note_analyzer_change(tx, analyzer.name, removed=False)
1389
1471
  applied += await apply_rebuilds(client, tx, rebuilds)
1390
1472
  warn_redefined(redefine, rebuilds)
1391
1473
  return applied
@@ -1398,8 +1480,12 @@ async def apply_analyzer_removal(client: Any, name: str, statement: str, *, tx:
1398
1480
  ``client.version()`` once. Below 3 (SurrealDB 2.x, the embedded engine), which accept the removal of an analyzer an
1399
1481
  index uses and then refuse every write to that table: ``INFO FOR DB``, then ``INFO FOR TABLE`` of every table
1400
1482
  (:func:`indexes_using`, committed state), and the first index using it, in table then index order, refused with
1401
- SurrealDB 3.x's own reason, before any DDL. From 3 on, the server refuses it itself, in the same words. Then the
1402
- statement, joining *tx* when given: ``Can't apply analyzer removal: '<name>' -> <reason>`` for a refusal.
1483
+ SurrealDB 3.x's own reason, before any DDL. An index ``remove_index(tx=…)`` removed earlier in *tx* is left out,
1484
+ although committed state still holds it (:func:`indexes_removed_in`, issue #323); an index queued earlier in a
1485
+ buffered *tx* is not seen. From 3 on, the server refuses it itself, in the same words, in the transaction. Then the
1486
+ statement, joining *tx* when given: ``Can't apply analyzer removal: '<name>' -> <reason>`` for a refusal. *tx*
1487
+ notes the removal once applied or queued (:func:`note_analyzer_change`): ``define_analyzer(tx=…)`` and
1488
+ ``define_indexes(tx=…)`` then count the analyzer as missing (issue #323).
1403
1489
 
1404
1490
  :raises SurrealDbError: an index uses the analyzer; a refused read or statement.
1405
1491
  :raises SurrealDbNotFoundError: ``REMOVE ANALYZER`` without ``IF EXISTS`` of a missing analyzer (outside a buffered
@@ -1408,13 +1494,14 @@ async def apply_analyzer_removal(client: Any, name: str, statement: str, *, tx:
1408
1494
  """
1409
1495
  if line_major(await client.version()) < 3:
1410
1496
  _, tables = await read_database_info(client, what="analyzer", kind="removal", subject=repr(name))
1411
- using = await indexes_using(client, tables, {name}, what="analyzer", kind="removal")
1497
+ using = await indexes_using(client, tables, {name}, what="analyzer", kind="removal", skip=indexes_removed_in(tx))
1412
1498
  if using:
1413
1499
  table, index, _ = using[0]
1414
1500
  raise SurrealDbError(analyzer_in_use_refused(name, index, table))
1415
1501
  await apply_ddl_statements(
1416
1502
  [statement], client=client, tx=tx, what="analyzer", kind="removal", classify=True, subjects=[repr(name)]
1417
1503
  )
1504
+ note_analyzer_change(tx, name, removed=True)
1418
1505
  return statement
1419
1506
 
1420
1507
 
@@ -32,8 +32,16 @@ records a transaction wrote before the index was defined. A raw ``tx.add()`` is
32
32
 
33
33
  Indexes removed (issue #312): ``remove_index(tx=…)`` notes the index it removes, by table (``_note_index_removal``).
34
34
  ``define_indexes(tx=…)`` reads committed state, where that index still stands: it counts the indexes noted for its table
35
- (``_indexes_removed``) as missing, plans them again, and forgets those it defines (``_forget_index_removals``). The
36
- commit and the cancel keep the note, like the tables written. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
35
+ (``_indexes_removed``) as missing, plans them again, and forgets those it defines (``_forget_index_removals``).
36
+ ``remove_analyzer(tx=…)`` and ``define_analyzer(tx=…)`` read committed state too, and leave the noted indexes out of
37
+ those that use the analyzer (issue #323). The commit and the cancel keep the note, like the tables written. A raw
38
+ ``tx.add("REMOVE INDEX …")`` is not noted.
39
+
40
+ Analyzers changed (issue #323): ``remove_analyzer(tx=…)`` notes the analyzer it removes (``_note_analyzer_removal``),
41
+ ``define_analyzer(tx=…)`` the one it defines or redefines (``_note_analyzer_definition``); the last change of a name
42
+ wins. ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)`` count a removed one as missing (``_analyzers_removed``),
43
+ and ``define_indexes(tx=…)`` refuses an index whose analyzer the block redefined (``_analyzers_defined``): SurrealDB
44
+ builds an index with the analyzers committed before the transaction began. The commit and the cancel keep the note.
37
45
 
38
46
  Errors (issue #278): the errors a failed transaction raises give the server's reason and the failed statement's
39
47
  position, without the server's excerpt of the statement; the server's own text stays reachable as their suppressed
@@ -240,6 +248,10 @@ class Transaction(ABC):
240
248
  # Issue #312: the indexes remove_index(tx=…) removed in this transaction, by table, until define_indexes(tx=…)
241
249
  # defines them again; kept until the transaction ends.
242
250
  self._removed_indexes: dict[str, set[str]] = {}
251
+ # Issue #323: the analyzers remove_analyzer(tx=…) removed and those define_analyzer(tx=…) defined or redefined
252
+ # in this transaction — the last change of a name wins; kept until the transaction ends.
253
+ self._removed_analyzers: set[str] = set()
254
+ self._defined_analyzers: set[str] = set()
243
255
  # Issue #268: the errors of the statements that failed in this transaction (interactive strategy).
244
256
  self._failures: list[BaseException] = []
245
257
 
@@ -304,8 +316,10 @@ class Transaction(ABC):
304
316
  """Note that the index *name* of *table* was removed in this transaction — issue #312.
305
317
 
306
318
  ``remove_index(tx=…)`` calls it once its ``REMOVE INDEX`` was applied (interactive) or queued (buffered).
307
- ``define_indexes(tx=…)``, which reads committed state where the index still stands, counts it as missing. Like
308
- the tables written, the commit and the cancel keep it. A raw ``tx.add()`` is not noted.
319
+ ``define_indexes(tx=…)``, which reads committed state where the index still stands, counts it as missing;
320
+ ``remove_analyzer(tx=…)`` and ``define_analyzer(tx=…)`` no longer count it among the indexes that use an
321
+ analyzer (issue #323). Like the tables written, the commit and the cancel keep it. A raw ``tx.add()`` is not
322
+ noted.
309
323
  """
310
324
  self._removed_indexes.setdefault(table, set()).add(name)
311
325
 
@@ -321,6 +335,35 @@ class Transaction(ABC):
321
335
  if removed is not None:
322
336
  removed.difference_update(names)
323
337
 
338
+ def _note_analyzer_removal(self, name: str) -> None:
339
+ """Note that the analyzer *name* was removed in this transaction — issue #323.
340
+
341
+ ``remove_analyzer(tx=…)`` calls it once its ``REMOVE ANALYZER`` was applied (interactive) or queued (buffered).
342
+ ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)``, which read committed state where the analyzer still
343
+ stands, count it as missing. A definition noted before for the same name is forgotten. Like the indexes
344
+ removed, the commit and the cancel keep it. A raw ``tx.add()`` is not noted.
345
+ """
346
+ self._defined_analyzers.discard(name)
347
+ self._removed_analyzers.add(name)
348
+
349
+ def _note_analyzer_definition(self, name: str) -> None:
350
+ """Note that the analyzer *name* was defined or redefined in this transaction — issue #323.
351
+
352
+ ``define_analyzer(tx=…)`` calls it once its ``DEFINE ANALYZER`` was applied or queued. ``define_indexes(tx=…)``
353
+ refuses to define an index that uses it: SurrealDB builds an index with the analyzers committed before the
354
+ transaction began. A removal noted before for the same name is forgotten. The commit and the cancel keep it.
355
+ """
356
+ self._removed_analyzers.discard(name)
357
+ self._defined_analyzers.add(name)
358
+
359
+ def _analyzers_removed(self) -> frozenset[str]:
360
+ """The analyzers removed in this transaction and not defined again since (issue #323)."""
361
+ return frozenset(self._removed_analyzers)
362
+
363
+ def _analyzers_defined(self) -> frozenset[str]:
364
+ """The analyzers defined or redefined in this transaction and not removed since (issue #323)."""
365
+ return frozenset(self._defined_analyzers)
366
+
324
367
  @property
325
368
  @abstractmethod
326
369
  def is_interactive(self) -> bool:
@@ -8,7 +8,11 @@ storage type is given (``F64``, ``F32``, ``I64``, ``I32`` or ``I16``, in any cas
8
8
  Pydantic reads the ``Vector`` marker exactly as ``VectorField[1536]``'s. Both spellings are one field: the same
9
9
  validation, the same column type (``option<array<float, 1536>>``), the same schema. ``get_vector_info()`` and
10
10
  ``is_vector_field()`` — the full ORM's helpers, under their names — read the marker of either spelling from a model
11
- field or an annotation.
11
+ field or an annotation, and of their unions with ``None`` (``VectorField[1536] | None``,
12
+ ``Annotated[list[float], Vector(1536)] | None``), as ``define_table()`` reads them; never from inside a container
13
+ (``list[VectorField[3]]``). A field or an annotation holding two different markers, or a marker next to a type other
14
+ than ``None``, has no single dimension: the helpers refuse it, and a model's ``class`` statement refuses such a field
15
+ (``declared_vectors()``).
12
16
 
13
17
  A value is ``None`` or a ``list``/``tuple`` of exactly ``d`` real numbers — ``int``, ``float``, any ``numbers.Real``,
14
18
  a ``decimal.Decimal`` (SurrealDB's decimals read back as ``Decimal``) —, never a ``bool`` nor a ``str``, each finite
@@ -50,7 +54,8 @@ from collections.abc import Mapping, Sequence
50
54
  from dataclasses import dataclass
51
55
  from decimal import Decimal
52
56
  from numbers import Real
53
- from typing import Annotated, Any, NoReturn, Self, TypedDict, get_args, get_origin
57
+ from types import NoneType, UnionType
58
+ from typing import Annotated, Any, NoReturn, Self, TypedDict, Union, get_args, get_origin
54
59
 
55
60
  from pydantic import Field, GetCoreSchemaHandler, GetJsonSchemaHandler
56
61
  from pydantic.fields import FieldInfo
@@ -292,38 +297,102 @@ class VectorField:
292
297
  return _ANNOTATED[list[float] | None, Vector(*checked), Field(default=None)]
293
298
 
294
299
 
295
- def _marker(annotation_or_field: Any) -> Vector | None:
296
- """The first ``Vector`` of a ``FieldInfo``'s metadata or of an ``Annotated`` annotation's, else ``None``."""
300
+ def _held(annotation: Any, metadata: Sequence[Any] = ()) -> tuple[tuple[Vector, ...], bool]:
301
+ """The distinct ``Vector`` markers that declare a field of *annotation* (with the field's own *metadata*), in order,
302
+ and whether the field also takes a type other than ``None`` next to them.
303
+
304
+ The ``Annotated`` layers are unwrapped and their metadata added to *metadata*: when it holds a ``Vector``, those
305
+ markers decide, whatever the type under them (a ``Vector`` validates the value itself). Otherwise ``None`` gives
306
+ neither; a union gives the markers and other types of its members, each read the same way; any other type is
307
+ another type — a container holding vectors (``list[VectorField[3]]``) included, whose arguments are never read.
308
+ This is the reading ``model_ddl`` gives the column its kind by, so the column and the checks agree.
309
+ """
310
+ while get_origin(annotation) is Annotated:
311
+ metadata = (*metadata, *annotation.__metadata__)
312
+ annotation = annotation.__origin__
313
+ markers = tuple(dict.fromkeys(item for item in metadata if isinstance(item, Vector)))
314
+ if markers:
315
+ return markers, False
316
+ if annotation is None or annotation is NoneType:
317
+ return (), False
318
+ origin = get_origin(annotation)
319
+ if origin is not Union and origin is not UnionType:
320
+ return (), True
321
+ members = [_held(member) for member in get_args(annotation)]
322
+ markers = tuple(dict.fromkeys(marker for held, _ in members for marker in held))
323
+ return markers, any(other for _, other in members)
324
+
325
+
326
+ def ambiguous_marker_refused(subject: str, markers: Sequence[Vector], other: bool) -> str:
327
+ """The refusal of *subject* — a field, or an annotation —, which holds several different *markers*, or a marker and
328
+ a type other than ``None`` (*other*): a vector field of no single dimension and storage type."""
329
+ held = repr(markers[-1]) if len(markers) == 1 else f"{', '.join(map(repr, markers[:-1]))} and {markers[-1]!r}"
330
+ if other:
331
+ held += " next to a type other than None"
332
+ return (
333
+ f"{subject} holds {held}; a vector field holds one Vector marker, and None at most. Write VectorField[d] alone "
334
+ "(it is already optional) or Annotated[list[float] | None, Vector(d)]."
335
+ )
336
+
337
+
338
+ def _marker(annotation_or_field: Any, *, subject: str | None = None) -> Vector | None:
339
+ """The ``Vector`` marker of a ``FieldInfo`` or of an annotation (``_held()``), ``None`` when it holds none.
340
+
341
+ Several different markers, or a marker next to a type other than ``None`` in a union, raise ``ValueError`` naming
342
+ *subject* (by default "The field" or "The annotation").
343
+ """
297
344
  if isinstance(annotation_or_field, FieldInfo):
298
- metadata: Sequence[Any] = annotation_or_field.metadata
299
- elif get_origin(annotation_or_field) is Annotated:
300
- metadata = annotation_or_field.__metadata__
345
+ markers, other = _held(annotation_or_field.annotation, annotation_or_field.metadata)
346
+ default = "The field"
301
347
  else:
348
+ markers, other = _held(annotation_or_field)
349
+ default = "The annotation"
350
+ if not markers:
302
351
  return None
303
- for item in metadata:
304
- if isinstance(item, Vector):
305
- return item
306
- if isinstance(annotation_or_field, FieldInfo):
307
- return None
308
- return _marker(get_args(annotation_or_field)[0])
352
+ if len(markers) > 1 or other:
353
+ raise ValueError(ambiguous_marker_refused(subject or default, markers, other))
354
+ return markers[0]
309
355
 
310
356
 
311
357
  def get_vector_info(annotation_or_field: Any) -> tuple[int, str] | None:
312
358
  """The ``(dimension, storage type)`` of a vector field, or ``None`` when it is not one (the full ORM's helper).
313
359
 
314
360
  Takes a model field (``Model.model_fields["embedding"]``, a Pydantic ``FieldInfo``) or an annotation
315
- (``VectorField[3]``, ``Annotated[list[float] | None, Vector(3)]``): both spellings give ``(3, "F32")``. Anything
316
- without a ``Vector`` marker — ``list[float]``, another field — gives ``None``.
361
+ (``VectorField[3]``, ``Annotated[list[float] | None, Vector(3)]``): both spellings give ``(3, "F32")``, and so do
362
+ their unions with ``None`` (``VectorField[3] | None``, ``Optional[VectorField[3]]``,
363
+ ``Annotated[list[float], Vector(3)] | None``), read as ``define_table()`` reads them. Anything without a ``Vector``
364
+ marker — ``list[float]``, another field, vectors held in a container such as ``list[VectorField[3]]`` — gives
365
+ ``None``.
366
+
367
+ :raises ValueError: the field or annotation holds two different ``Vector`` markers, or a marker next to a type
368
+ other than ``None`` in a union (``VectorField[3] | str``): its dimension and storage type are not one. A
369
+ model's ``class`` statement refuses such a field already, naming it.
317
370
  """
318
371
  marker = _marker(annotation_or_field)
319
372
  return None if marker is None else (marker.dimension, marker.vector_type)
320
373
 
321
374
 
322
375
  def is_vector_field(annotation_or_field: Any) -> bool:
323
- """Whether a model field or an annotation is a vector field, either spelling (the full ORM's helper)."""
376
+ """Whether a model field or an annotation is a vector field, any spelling — a union with ``None`` included — (the
377
+ full ORM's helper). Refuses what ``get_vector_info()`` refuses, with its ``ValueError``."""
324
378
  return _marker(annotation_or_field) is not None
325
379
 
326
380
 
381
+ def declared_vectors(owner: str, fields: Mapping[str, FieldInfo]) -> dict[str, tuple[int, str]]:
382
+ """The ``(dimension, storage type)`` of each vector field among a model's *fields*, by field name, as
383
+ ``get_vector_info()`` reads them: what the model's ``class`` statement checks (issue #318).
384
+
385
+ A field whose ``Vector`` markers are ambiguous raises ``ValueError`` naming the model *owner* and the field, so the
386
+ ``class`` statement refuses it whether or not the model declares indexes.
387
+ """
388
+ declared: dict[str, tuple[int, str]] = {}
389
+ for name, field in fields.items():
390
+ marker = _marker(field, subject=f"{owner}: field {name!r}")
391
+ if marker is not None:
392
+ declared[name] = (marker.dimension, marker.vector_type)
393
+ return declared
394
+
395
+
327
396
  # --- the checks -----------------------------------------------------------------------------------------------------
328
397
 
329
398