From 4713e88fe759e87a0664ae8debb820b83e15e9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Fri, 3 Oct 2025 23:15:53 +0300 Subject: [PATCH] Fix documentation related to views and SQLite (#57) --- docs/views.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/docs/views.md b/docs/views.md index 6bd4676..91ccc9b 100644 --- a/docs/views.md +++ b/docs/views.md @@ -71,9 +71,15 @@ create_view(conn).value(); This generates different SQL depending on the database: - **MySQL/PostgreSQL**: `CREATE OR REPLACE VIEW "EMPLOYEE_SUMMARY" AS SELECT ...` -- **SQLite**: `CREATE VIEW IF NOT EXISTS "EmployeeView" AS SELECT ...` (automatically translated) +- **SQLite**: `CREATE VIEW "EmployeeView" AS SELECT ...` -**Important**: SQLite does not support `CREATE OR REPLACE VIEW`. When you use `create_or_replace_view_as` with SQLite, sqlgen automatically translates this to `CREATE VIEW IF NOT EXISTS` syntax. +**Important**: SQLite does not support `CREATE OR REPLACE VIEW`. When you use `create_or_replace_view_as` with SQLite, sqlgen does not automatically translate this and will result in an error if the view already exists. The recommended way to achieve this is to manually drop the view before creating it: + +```cpp +(drop | if_exists)(conn).value(); +const auto create_view = create_as(employee_query); +create_view(conn).value(); +``` ### View Creation with if_not_exists @@ -200,15 +206,7 @@ CREATE OR REPLACE VIEW "EMPLOYEE_SUMMARY" AS SELECT ...; ### SQLite -SQLite has different view creation behavior: - -```sql --- SQLite does NOT support CREATE OR REPLACE VIEW --- Instead, it uses CREATE VIEW IF NOT EXISTS -CREATE VIEW IF NOT EXISTS "EmployeeView" AS SELECT ...; -``` - -**Important**: SQLite does not support `CREATE OR REPLACE VIEW`. When you use `create_or_replace_view_as` with SQLite, sqlgen automatically translates this to the appropriate SQLite syntax. For better SQLite compatibility, use `create_as(query) | if_not_exists` instead. +SQLite has different view creation behavior. It does not support `CREATE OR REPLACE VIEW`. Instead, you should manually drop the view if it exists before creating it. See the example in the "Create or Replace View" section. ## Best Practices