Skip to content

Commit b10f2ca

Browse files
authored
docs: explicitly recommend uuid to generate PKs (#182)
Towards #181
1 parent 4c610ea commit b10f2ca

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ metadata.create_all(engine)
5858

5959
### Insert a row
6060
```python
61+
import uuid
62+
6163
from sqlalchemy import (
6264
MetaData,
6365
Table,
@@ -68,9 +70,10 @@ engine = create_engine(
6870
"spanner:///projects/project-id/instances/instance-id/databases/database-id"
6971
)
7072
user = Table("users", MetaData(bind=engine), autoload=True)
73+
user_id = uuid.uuid4().hex[:6].lower()
7174

7275
with engine.begin() as connection:
73-
connection.execute(user.insert(), {"user_id": 1, "user_name": "Full Name"})
76+
connection.execute(user.insert(), {"user_id": user_id, "user_name": "Full Name"})
7477
```
7578

7679
### Read
@@ -158,7 +161,9 @@ autocommit_engine = eng.execution_options(isolation_level="AUTOCOMMIT")
158161
```
159162

160163
### Autoincremented IDs
161-
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:
164+
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)). We recommend that you use the Python [uuid](https://docs.python.org/3/library/uuid.html) module to generate primary key fields to avoid creating monotonically increasing keys.
165+
166+
Though it's not encouraged to do so, in case you *need* the feature, you can simulate it manually as follows:
162167
```python
163168
with engine.begin() as connection:
164169
top_id = connection.execute(

0 commit comments

Comments
 (0)