mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-08 01:24:20 +02:00
Make tables in attached/temp schemas first-class (issue #432)
Table/View gain an explicit alias attribute for their attached or temp schema. db["alias.table"] resolves it when alias names a real attached database or the temp schema (schema_names), leaving plain and literally-dotted main-schema names unaffected. exists(), columns, schema, indexes, foreign_keys, triggers, and the read/write path (rows_where, get, insert/insert_all, upsert, update, delete, delete_where, add_column, drop, analyze) are all schema-qualified correctly instead of silently checking/writing the main schema. Methods that would need deeper cross-schema rework (create, transform, create_index, enable_fts and friends, add_foreign_key, extract, duplicate, spatial columns) raise a clear NotImplementedError for aliased tables instead of silently operating on a same-named main-schema table.
This commit is contained in:
parent
050f11f31f
commit
fe5260e47e
3 changed files with 263 additions and 49 deletions
|
|
@ -184,6 +184,21 @@ You can attach an additional database using the ``.attach()`` method, providing
|
|||
|
||||
You can reference tables in the attached database using the alias value you passed to ``db.attach(alias, filepath)`` as a prefix, for example the ``second.table_in_second`` reference in the SQL query above.
|
||||
|
||||
The same ``alias.table`` syntax works with ``db[...]`` to get back a :class:`.Table` object for a table in the attached database, with ``.alias`` set to the attached database's alias:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
table = db["second.table_in_second"]
|
||||
table.alias # "second"
|
||||
table.exists() # True, if it exists in second.db
|
||||
list(table.rows_where("id > ?", [10]))
|
||||
table.insert({"id": 20, "name": "example"})
|
||||
table.delete_where("id = ?", [20])
|
||||
|
||||
Core operations - ``exists()``, ``columns``, ``schema``, ``rows_where()``, ``get()``, ``insert()``/``insert_all()``, ``update()``, ``delete()``/``delete_where()``, ``add_column()`` and ``drop()`` - all work against attached tables this way, as does the ``temp`` schema for tables created with ``CREATE TEMPORARY TABLE``, for example ``db["temp.scratch"]``.
|
||||
|
||||
Schema-mutating methods that are not yet supported for attached or temp tables - such as ``.create()``, ``.transform()``, ``.create_index()`` and ``.enable_fts()`` - raise ``NotImplementedError`` rather than silently operating on a same-named table in the main schema.
|
||||
|
||||
.. note::
|
||||
In the CLI: :ref:`sqlite-utils --attach <cli_query_attach>`
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue