From 2ded635b360d78debde972d0f52616db4dd0b153 Mon Sep 17 00:00:00 2001 From: Ilya Gurov Date: Thu, 23 Dec 2021 13:59:23 +0300 Subject: [PATCH] docs: add a README section for the autoincremented ids --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 28d09eee..b228a680 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,18 @@ eng = create_engine("spanner:///projects/project-id/instances/instance-id/databa autocommit_engine = eng.execution_options(isolation_level="AUTOCOMMIT") ``` +### Autoincremented IDs +Cloud Spanner doesn't support autoincremented IDs mechanism due to performance reasons ([see for more details](https://cloud.google.com/spanner/docs/schema-design#primary-key-prevent-hotspots)). Though it's not encouraged to do so, in case you *need* the feature, you can simulate it manually, for example: +```python +with engine.begin() as connection: + top_id = connection.execute( + select([user.c.user_id]).order_by(user.c.user_id.desc()).limit(1) + ).fetchone() + next_id = top_id[0] + 1 if top_id else 1 + + connection.execute(user.insert(), {"user_id": next_id}) +``` + ### Query hints Spanner dialect supports [query hints](https://cloud.google.com/spanner/docs/query-syntax#table_hints), which give the ability to set additional query execution parameters. Usage example: ```python