From 9f25d231c618014176d7b66d494b26009eea5509 Mon Sep 17 00:00:00 2001 From: Alessio Giuliano Date: Fri, 22 May 2026 14:39:55 +0200 Subject: [PATCH] Fallback to DROP VIEW if DROP TABLE fails in DuckDB Signed-off-by: Alessio Giuliano --- sqlmesh/core/engine_adapter/duckdb.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sqlmesh/core/engine_adapter/duckdb.py b/sqlmesh/core/engine_adapter/duckdb.py index ebfcaa7901..38a22300cb 100644 --- a/sqlmesh/core/engine_adapter/duckdb.py +++ b/sqlmesh/core/engine_adapter/duckdb.py @@ -218,3 +218,23 @@ def _create_table( @property def _is_motherduck(self) -> bool: return self._extra_config.get("is_motherduck", False) + + def drop_table( + self, + table_name: t.Any, + exists: bool = True, + **kwargs: t.Any, + ) -> None: + """ + DuckDB will raise an error if you try to DROP TABLE on a view. + Fallback to DROP VIEW if the execution of DROP TABLE fails. + """ + from duckdb import Error + + # Safety: Remove 'exists' from kwargs so we don't pass it twice + kwargs.pop("exists", None) + + try: + super().drop_table(table_name, exists=exists, **kwargs) + except Error: + self.drop_view(table_name, **kwargs)