@@ -198,6 +198,45 @@ In this example, we:
198198 The SQLAlchemy documentation has more information about `working with
199199 tables `_.
200200
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+
201240.. _using-extension-types :
202241
203242Extension types
@@ -336,11 +375,9 @@ You can then set the values of the ``Geopoint`` and ``Geoshape`` columns::
336375 >>> session.add(tokyo)
337376 >>> session.commit()
338377
339-
340378Querying
341379========
342380
343-
344381When the ``commit `` method is called, two ``INSERT `` statements are sent to
345382CrateDB. However, the newly inserted rows aren't immediately available for
346383querying 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
544581column, meaning that it doesn't exist on the source table, and in most cases,
545582should not be included in your :ref: `table definition <table-definition >`.
546583
547- You can select ``_score `` as part of a query, like this:
584+ You can select ``_score `` as part of a query, like this::
548585
549586 >>> session.query(Character.name, '_score') \
550587 ... .filter(match(Character.quote_ft, 'space')) \
@@ -557,6 +594,7 @@ table definition But notice that we select the associated score by passing in
557594the virtual column name as a string (``_score ``) instead of using a defined
558595column on the ``Character `` class.
559596
597+
560598.. _SQLAlchemy : http://www.sqlalchemy.org/
561599.. _Object-Relational Mapping : https://en.wikipedia.org/wiki/Object-relational_mapping
562600.. _dialect : http://docs.sqlalchemy.org/en/latest/dialects/
0 commit comments