{"html_url": "https://github.com/simonw/sqlite-utils/issues/432#issuecomment-1155764428", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/432", "id": 1155764428, "node_id": "IC_kwDOCGYnMM5E45DM", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-06-14T22:16:21Z", "updated_at": "2022-06-14T22:16:21Z", "author_association": "OWNER", "body": "Initial idea of how the `.table()` method would change:\r\n```diff\r\ndiff --git a/sqlite_utils/db.py b/sqlite_utils/db.py\r\nindex 7a06304..3ecb40b 100644\r\n--- a/sqlite_utils/db.py\r\n+++ b/sqlite_utils/db.py\r\n@@ -474,11 +474,12 @@ class Database:\r\n self._tracer(sql, None)\r\n return self.conn.executescript(sql)\r\n \r\n- def table(self, table_name: str, **kwargs) -> Union[\"Table\", \"View\"]:\r\n+ def table(self, table_name: str, alias: Optional[str] = None, **kwargs) -> Union[\"Table\", \"View\"]:\r\n \"\"\"\r\n Return a table object, optionally configured with default options.\r\n \r\n :param table_name: Name of the table\r\n+ :param alias: The database alias to use, if referring to a table in another connected database\r\n \"\"\"\r\n klass = View if table_name in self.view_names() else Table\r\n return klass(self, table_name, **kwargs)\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1236693079, "label": "Support `rows_where()`, `delete_where()` etc for attached alias databases"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/432#issuecomment-1155764064", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/432", "id": 1155764064, "node_id": "IC_kwDOCGYnMM5E449g", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-06-14T22:15:44Z", "updated_at": "2022-06-14T22:15:44Z", "author_association": "OWNER", "body": "Implementing this would be a pretty big change - initial instinct is that I'd need to introduce a `self.alias` property to `Queryable` (the subclass of `Table` and `View`) and a new `self.name_with_alias` getter which returns `alias.tablename` if `alias` is set to a not-None value. Then I'd need to rewrite every piece of code like this:\r\n\r\nhttps://github.com/simonw/sqlite-utils/blob/1b09538bc6c1fda773590f3e600993ef06591041/sqlite_utils/db.py#L1161\r\n\r\nTo look like this instead:\r\n```python\r\n sql = \"select {} from [{}]\".format(select, self.name_with_alias)\r\n```\r\nBut some parts would be harder - for example:\r\n\r\nhttps://github.com/simonw/sqlite-utils/blob/1b09538bc6c1fda773590f3e600993ef06591041/sqlite_utils/db.py#L1227-L1231\r\n\r\nWould have to know to query `alias.sqlite_master` instead.\r\n\r\nThe cached table counts logic like this would need a bunch of changes too:\r\n\r\nhttps://github.com/simonw/sqlite-utils/blob/1b09538bc6c1fda773590f3e600993ef06591041/sqlite_utils/db.py#L644-L657", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1236693079, "label": "Support `rows_where()`, `delete_where()` etc for attached alias databases"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/432#issuecomment-1155759857", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/432", "id": 1155759857, "node_id": "IC_kwDOCGYnMM5E437x", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-06-14T22:09:07Z", "updated_at": "2022-06-14T22:09:07Z", "author_association": "OWNER", "body": "Third option, and I think the one I like the best:\r\n```python\r\nrows = db.table(\"tablename\", alias=\"otherdb\").rows_where(alias=\"otherdb\")\r\n```\r\nThe `db.table(tablename)` method already exists as an alternative to `db[tablename]`: https://sqlite-utils.datasette.io/en/stable/python-api.html#python-api-table-configuration\r\n", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1236693079, "label": "Support `rows_where()`, `delete_where()` etc for attached alias databases"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/432#issuecomment-1155758664", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/432", "id": 1155758664, "node_id": "IC_kwDOCGYnMM5E43pI", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-06-14T22:07:50Z", "updated_at": "2022-06-14T22:07:50Z", "author_association": "OWNER", "body": "Another potential fix: add a `alias=` parameter to `rows_where()` and other similar methods. Then you could do this:\r\n\r\n```python\r\nrows = db[\"tablename\"].rows_where(alias=\"otherdb\")\r\n```\r\nThis feels wrong to me: `db[\"tablename\"]` is the bit that is supposed to return a table object. Having part of what that table object is exist as a parameter to other methods is confusing.\r\n", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1236693079, "label": "Support `rows_where()`, `delete_where()` etc for attached alias databases"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/432#issuecomment-1155756742", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/432", "id": 1155756742, "node_id": "IC_kwDOCGYnMM5E43LG", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-06-14T22:05:38Z", "updated_at": "2022-06-14T22:05:49Z", "author_association": "OWNER", "body": "I don't like the idea of `table_names()` returning names of tables from connected databases as well, because it feels like it could lead to surprising behaviour - especially if those connected databases turn to have table names that are duplicated in the main connected database.\r\n\r\nIt would be neat if functions like `.rows_where()` worked though.\r\n\r\nOne thought would be to support something like this:\r\n```python\r\nrows = db[\"otherdb.tablename\"].rows_where()\r\n```\r\nBut... `.` is a valid character in a SQLite table name. So `\"otherdb.tablename\"` might ambiguously refer to a table called `tablename` in a connected database with the alias `otherdb`, OR a table in the current database with the name `otherdb.tablename`.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1236693079, "label": "Support `rows_where()`, `delete_where()` etc for attached alias databases"}, "performed_via_github_app": null}