You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/database_queries.md
+48-9Lines changed: 48 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,48 @@ notes = sqlalchemy.Table(
24
24
)
25
25
```
26
26
27
-
You can use any of the sqlalchemy column types such as `sqlalchemy.JSON`, or
27
+
You can use any of the SQLAlchemy column types such as `sqlalchemy.JSON`, or
28
28
custom column types.
29
29
30
+
## Creating tables
31
+
32
+
Databases doesn't use SQLAlchemy's engine for database access internally. [The usual SQLAlchemy core way to create tables with `create_all`](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.MetaData.create_all) is therefore not available. To work around this you can use SQLAlchemy to [compile the query to SQL](https://docs.sqlalchemy.org/en/20/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined) and then execute it with databases:
Note that this way of creating tables is only useful for local experimentation. For serious projects, we recommend using a proper database schema migrations solution like [Alembic](https://alembic.sqlalchemy.org/en/latest/).
68
+
30
69
## Queries
31
70
32
71
You can now use any [SQLAlchemy core][sqlalchemy-core] queries ([official tutorial][sqlalchemy-core-tutorial]).
@@ -70,11 +109,11 @@ query = notes.select()
70
109
asyncfor row in database.iterate(query=query):
71
110
...
72
111
73
-
# Close all connection in the connection pool
112
+
# Close all connections in the connection pool
74
113
await database.disconnect()
75
114
```
76
115
77
-
Connections are managed as task-local state, with driver implementations
116
+
Connections are managed as a task-local state, with driver implementations
78
117
transparently using connection pooling behind the scenes.
79
118
80
119
## Raw queries
@@ -111,17 +150,17 @@ Note that query arguments should follow the `:query_arg` style.
111
150
112
151
## Query result
113
152
114
-
To keep in line with [SQLAlchemy 1.4 changes][sqlalchemy-mapping-changes]
115
-
query result object no longer implements a mapping interface.
116
-
To access query result as a mapping you should use the `_mapping` property.
117
-
That way you can process both SQLAlchemy Rows and databases Records from raw queries
153
+
To keep in line with [SQLAlchemy 1.4 changes][sqlalchemy-mapping-changes]
154
+
query result object no longer implements a mapping interface.
155
+
To access query result as a mapping you should use the `_mapping` property.
156
+
That way you can process both SQLAlchemy Rows and databases Records from raw queries
118
157
with the same function without any instance checks.
119
158
120
159
```python
121
160
query ="SELECT * FROM notes WHERE id = :id"
122
161
result =await database.fetch_one(query=query, values={"id": 1})
0 commit comments