The dbt model runs fine when it is materialized as a table but does not run when it is materialized as a view and shows the following output:
Database Error in {model name}
failed to find conversion function from "unknown" to character varying
Database Error in {model name}
failed to find conversion function from "unknown" to character varying
This issue usually happens because some database engines handle implicit type conversion differently between views and tables. When materializing as a table, dbt executes the SQL and stores the result as a static table, ensuring that all data types are resolved.
However, views are just stored SQL queries, and the database tries to infer types dynamically during execution, which can lead to issues with implicit conversions. Since you’ve already tried type casting, double-check that all columns explicitly define their types, especially any NULL values or CASE statements, which can default to unknown in some databases. Try wrapping problematic fields with CAST(value AS VARCHAR) or ensuring that all branches of CASE return the same type.