github
html_url | issue_url | id | node_id | user | created_at | updated_at | author_association | body | reactions | issue | performed_via_github_app |
---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/simonw/datasette/issues/1878#issuecomment-1336100218 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336100218 | IC_kwDOBm6k_c5Po0V6 | 9599 | 2022-12-03T07:02:15Z | 2022-12-03T07:02:15Z | OWNER | Moved this work to a PR: - #1931 | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 | |
https://github.com/simonw/datasette/issues/1878#issuecomment-1336094562 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336094562 | IC_kwDOBm6k_c5Poy9i | 9599 | 2022-12-03T06:27:50Z | 2022-12-03T06:29:06Z | OWNER | This adds it to the API explorer: ```diff diff --git a/datasette/views/special.py b/datasette/views/special.py index 1f84b094..1b4a9d3c 100644 --- a/datasette/views/special.py +++ b/datasette/views/special.py @@ -316,21 +316,37 @@ class ApiExplorerView(BaseView): request.actor, "insert-row", (name, table) ): pks = await db.primary_keys(table) - table_links.append( - { - "path": self.ds.urls.table(name, table) + "/-/insert", - "method": "POST", - "label": "Insert rows into {}".format(table), - "json": { - "rows": [ - { - column: None - for column in await db.table_columns(table) - if column not in pks - } - ] + table_links.extend( + [ + { + "path": self.ds.urls.table(name, table) + "/-/insert", + "method": "POST", + "label": "Insert rows into {}".format(table), + "json": { + "rows": [ + { + column: None + for column in await db.table_columns(table) + if column not in pks + } + ] + }, }, - } + { + "path": self.ds.urls.table(… | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 | |
https://github.com/simonw/datasette/issues/1878#issuecomment-1336094470 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336094470 | IC_kwDOBm6k_c5Poy8G | 9599 | 2022-12-03T06:27:13Z | 2022-12-03T06:27:13Z | OWNER | Tests are going to need to cover both rowid-only and compound primary key tables, including all of the error states. | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 | |
https://github.com/simonw/datasette/issues/1878#issuecomment-1336094381 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336094381 | IC_kwDOBm6k_c5Poy6t | 9599 | 2022-12-03T06:26:25Z | 2022-12-03T06:26:25Z | OWNER | Initial prototype: ```diff diff --git a/datasette/app.py b/datasette/app.py index 125b4969..282c0984 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -40,7 +40,7 @@ from .views.special import ( PermissionsDebugView, MessagesDebugView, ) -from .views.table import TableView, TableInsertView, TableDropView +from .views.table import TableView, TableInsertView, TableUpsertView, TableDropView from .views.row import RowView, RowDeleteView, RowUpdateView from .renderer import json_renderer from .url_builder import Urls @@ -1292,6 +1292,10 @@ class Datasette: TableInsertView.as_view(self), r"/(?P<database>[^\/\.]+)/(?P<table>[^\/\.]+)/-/insert$", ) + add_route( + TableUpsertView.as_view(self), + r"/(?P<database>[^\/\.]+)/(?P<table>[^\/\.]+)/-/upsert$", + ) add_route( TableDropView.as_view(self), r"/(?P<database>[^\/\.]+)/(?P<table>[^\/\.]+)/-/drop$", diff --git a/datasette/views/table.py b/datasette/views/table.py index 7ba78c11..ae0d6366 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -1074,9 +1074,15 @@ class TableInsertView(BaseView): def __init__(self, datasette): self.ds = datasette - async def _validate_data(self, request, db, table_name): + async def _validate_data(self, request, db, table_name, pks, upsert): errors = [] + pks_list = [] + if isinstance(pks, str): + pks_list = [pks] + else: + pks_list = list(pks) + def _errors(errors): return None, errors, {} @@ -1135,6 +1141,15 @@ class TableInsertView(BaseView): # Validate columns of each row columns = set(await db.table_columns(table_name)) for i, row in enumerate(rows): + if upsert: + # It MUST have the primary key + missing_pks = [pk for pk in pks_list if pk not in row] + if missi… | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 | |
https://github.com/simonw/datasette/issues/1878#issuecomment-1336073212 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336073212 | IC_kwDOBm6k_c5Potv8 | 9599 | 2022-12-03T05:38:49Z | 2022-12-03T05:38:49Z | OWNER | And on Discord today: https://discord.com/channels/823971286308356157/823971286941302908/1048426072066236536 | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 | |
https://github.com/simonw/datasette/issues/1878#issuecomment-1336070843 | https://api.github.com/repos/simonw/datasette/issues/1878 | 1336070843 | IC_kwDOBm6k_c5PotK7 | 9599 | 2022-12-03T05:37:53Z | 2022-12-03T05:37:53Z | OWNER | Also requested here: https://news.ycombinator.com/item?id=33839894 | { "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
1432013704 |