1818 from dbt .adapters .base import BaseRelation
1919 from dbt .adapters .base .column import Column
2020 from dbt .adapters .base .impl import AdapterResponse
21+ from sqlmesh .core .engine_adapter .base import DataObject
2122 from sqlmesh .dbt .relation import Policy
2223
2324
@@ -259,6 +260,10 @@ def load_relation(self, relation: BaseRelation) -> t.Optional[BaseRelation]:
259260 if not self .engine_adapter .table_exists (mapped_table ):
260261 return None
261262
263+ data_object = self .engine_adapter .get_data_object (mapped_table )
264+ if data_object is not None :
265+ return self ._data_object_to_relation (data_object )
266+
262267 return self ._table_to_relation (mapped_table )
263268
264269 def list_relations (self , database : t .Optional [str ], schema : str ) -> t .List [BaseRelation ]:
@@ -269,24 +274,10 @@ def list_relations(self, database: t.Optional[str], schema: str) -> t.List[BaseR
269274 return self .list_relations_without_caching (self ._table_to_relation (target_schema ))
270275
271276 def list_relations_without_caching (self , schema_relation : BaseRelation ) -> t .List [BaseRelation ]:
272- from sqlmesh .dbt .relation import RelationType
273-
274277 schema = self ._normalize (self ._schema (schema_relation ))
275278
276279 relations = [
277- self .relation_type .create (
278- database = do .catalog ,
279- schema = do .schema_name ,
280- identifier = do .name ,
281- quote_policy = self .quote_policy ,
282- # DBT relation types aren't snake case and instead just one word without spaces so we remove underscores
283- type = (
284- RelationType .External
285- if do .type .is_unknown
286- else RelationType (do .type .lower ().replace ("_" , "" ))
287- ),
288- )
289- for do in self .engine_adapter .get_data_objects (schema )
280+ self ._data_object_to_relation (do ) for do in self .engine_adapter .get_data_objects (schema )
290281 ]
291282 return relations
292283
@@ -381,6 +372,22 @@ def _map_table_name(self, table: exp.Table) -> exp.Table:
381372 def _relation_to_table (self , relation : BaseRelation ) -> exp .Table :
382373 return exp .to_table (relation .render (), dialect = self .project_dialect )
383374
375+ def _data_object_to_relation (self , data_object : DataObject ) -> BaseRelation :
376+ from sqlmesh .dbt .relation import RelationType
377+
378+ return self .relation_type .create (
379+ database = data_object .catalog ,
380+ schema = data_object .schema_name ,
381+ identifier = data_object .name ,
382+ quote_policy = self .quote_policy ,
383+ # DBT relation types aren't snake case and instead just one word without spaces so we remove underscores
384+ type = (
385+ RelationType .External
386+ if data_object .type .is_unknown
387+ else RelationType (data_object .type .lower ().replace ("_" , "" ))
388+ ),
389+ )
390+
384391 def _table_to_relation (self , table : exp .Table ) -> BaseRelation :
385392 return self .relation_type .create (
386393 database = table .catalog or None ,
0 commit comments