Skip to content

Commit

Permalink
Documentation: Show how to delete records with SQLAlchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 9, 2022
1 parent f016016 commit f294e3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/by-example/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ its corresponding API interfaces, see also :ref:`sqlalchemy-support`.
:maxdepth: 1

sqlalchemy/getting-started
sqlalchemy/cru
sqlalchemy/crud
sqlalchemy/inspection-reflection


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
========================================
SQLAlchemy: Create, retrieve, and update
========================================
================================================
SQLAlchemy: Create, retrieve, update, and delete
================================================

This section of the documentation, related to CrateDB's SQLAlchemy integration,
focuses on showing specific details when querying, inserting, and updating
records.
focuses on showing specific details when querying, inserting, updating, and
deleting records.

It exercises filtering and limiting, inserting and updating with default values,
and updating complex data types with nested Python dictionaries.
Expand All @@ -22,9 +22,9 @@ Import the relevant symbols:

>>> import sqlalchemy as sa
>>> from datetime import datetime
>>> from sqlalchemy import delete, func, text
>>> from sqlalchemy.ext.declarative import declarative_base
>>> from sqlalchemy.orm import sessionmaker
>>> from sqlalchemy.sql import text
>>> from crate.client.sqlalchemy.types import ObjectArray

Establish a connection to the database:
Expand Down Expand Up @@ -196,7 +196,7 @@ Update multiple records:
... loc.name = 'Ort %d' % x
... loc.kind = 'Update'
... session.add(loc)
... session.flush()
>>> session.flush()

Refresh table:

Expand Down Expand Up @@ -277,6 +277,25 @@ Refresh "characters" table:
{'name': {'first': 'Trillian', 'last': 'Dent'}, 'size': 45}


Delete
======

Deleting a record with SQLAlchemy works like this.

>>> session.query(Location).count()
24

>>> location = session.query(Location).first()
>>> session.delete(location)
>>> session.commit()
>>> session.flush()

>>> _ = connection.execute("REFRESH TABLE locations")

>>> session.query(Location).count()
23


.. hidden: Disconnect from database
>>> session.close()
Expand Down

0 comments on commit f294e3b

Please sign in to comment.