@@ -198,6 +198,45 @@ In this example, we:
198
198
The SQLAlchemy documentation has more information about `working with
199
199
tables `_.
200
200
201
+ ``_id `` as primary key
202
+ ......................
203
+
204
+ As with version 4.2 CrateDB supports the ``RETURNING `` clause, which makes it
205
+ possible to use the ``_id `` column as fetched value for the ``PRIMARY KEY ``
206
+ constraint, since the SQLAlchemy ORM always **requires ** a primary key.
207
+
208
+ A table schema like this
209
+
210
+ .. code-block :: sql
211
+
212
+ CREATE TABLE "doc"."logs" (
213
+ "ts" TIMESTAMP WITH TIME ZONE,
214
+ "level" TEXT,
215
+ "message" TEXT
216
+ )
217
+
218
+ would translate into the following declarative model::
219
+
220
+ >>> from sqlalchemy.schema import FetchedValue
221
+
222
+ >>> class Log(Base):
223
+ ...
224
+ ... __tablename__ = 'logs'
225
+ ... __mapper_args__ = {
226
+ ... 'exclude_properties': ['id']
227
+ ... }
228
+ ...
229
+ ... id = sa.Column("_id", sa.String, server_default=FetchedValue(), primary_key=True)
230
+ ... ts = sa.Column(sa.DateTime, server_default=sa.func.current_timestamp())
231
+ ... level = sa.Column(sa.String)
232
+ ... message = sa.Column(sa.String)
233
+
234
+ >>> log = Log(level="info", message="Hello World")
235
+ >>> session.add(log)
236
+ >>> session.commit()
237
+ >>> log.id
238
+ ...
239
+
201
240
.. _using-extension-types :
202
241
203
242
Extension types
@@ -336,11 +375,9 @@ You can then set the values of the ``Geopoint`` and ``Geoshape`` columns::
336
375
>>> session.add(tokyo)
337
376
>>> session.commit()
338
377
339
-
340
378
Querying
341
379
========
342
380
343
-
344
381
When the ``commit `` method is called, two ``INSERT `` statements are sent to
345
382
CrateDB. However, the newly inserted rows aren't immediately available for
346
383
querying because the table index is only updated periodically (one second, by
@@ -544,7 +581,7 @@ The score is made available via the ``_score`` column, which is a virtual
544
581
column, meaning that it doesn't exist on the source table, and in most cases,
545
582
should not be included in your :ref: `table definition <table-definition >`.
546
583
547
- You can select ``_score `` as part of a query, like this:
584
+ You can select ``_score `` as part of a query, like this::
548
585
549
586
>>> session.query(Character.name, '_score') \
550
587
... .filter(match(Character.quote_ft, 'space')) \
@@ -557,6 +594,7 @@ table definition But notice that we select the associated score by passing in
557
594
the virtual column name as a string (``_score ``) instead of using a defined
558
595
column on the ``Character `` class.
559
596
597
+
560
598
.. _SQLAlchemy : http://www.sqlalchemy.org/
561
599
.. _Object-Relational Mapping : https://en.wikipedia.org/wiki/Object-relational_mapping
562
600
.. _dialect : http://docs.sqlalchemy.org/en/latest/dialects/
0 commit comments