You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#### Accessing model, physical table, and virtual layer view names
1685
1685
1686
-
The physical table name of the invoking model can be accessed within a Python macro function through its evaluation context's `this_model` property.
1686
+
All SQLMesh models have a name in their `MODEL` specification. We refer to that as the model's "unqualified" and "unresolved" name because it may not correspond to any specific object in the SQL engine.
1687
+
1688
+
When SQLMesh renders and executes a model, it converts the model name into three forms at different stages:
1689
+
1690
+
1. The *fully qualified* name
1691
+
1692
+
- If the model name is of the form `schema.table`, SQLMesh determines the correct catalog and adds it, like `catalog.schema.table`
1693
+
- SQLMesh quotes each component of the name using the SQL engine's quoting and case-sensitivity rules, like `"catalog"."schema"."table"`
1694
+
1695
+
2. The *resolved* physical table name
1696
+
1697
+
- The qualified name of the model's underlying physical table
1698
+
1699
+
3. The *resolved* virtual layer view name
1700
+
1701
+
- The qualified name of the model's virtual layer view in the environment where the model is being executed
1702
+
1703
+
You can access any of these three forms in a Python macro through properties of the `evaluation` context object.
1704
+
1705
+
Access the unresolved, fully-qualified name through the `this_model_fqn` property.
# Value returned here: '"datalake"."landing"."customers"'
1715
+
unresolved_model_fqn = evaluator.this_model_fqn
1694
1716
...
1695
1717
```
1696
1718
1697
-
!!! note
1698
-
During the "promotion" runtime stage, the model name resolution occurs for the virtual layer. This means that `this_model` resolve to the qualified view name of the invoking model. For instance, when running the plan in an environment named `dev`, `db.test_model` and `this_model` would resolve to `'"db__dev"."test_model"'` and not to the physical table name.
1719
+
Access the resolved physical table and virtual layerview names through the `this_model` property.
1720
+
1721
+
The `this_model` property returns different names depending on the runtime stage:
1722
+
1723
+
-`promoting` runtime stage: `this_model` resolves to the virtual layer view name
1724
+
1725
+
- Example
1726
+
- Model name is `db.test_model`
1727
+
- `plan` is running in the `dev` environment
1728
+
- `this_model` resolves to `"catalog"."db__dev"."test_model"` (note the `__dev` suffix in the schema name)
1699
1729
1700
-
#### Accessing the invoking model's unresolved name
1730
+
- All other runtime stages: `this_model` resolves to the physical table name
1701
1731
1702
-
The unresolved, fully-qualified name of the invoking model can be accessed within a Python macro function through its evaluation context's `this_model_fqn` property.
1732
+
- Example
1733
+
- Model name is `db.test_model`
1734
+
- `plan` is running in any environment
1735
+
- `this_model` resolves to `"catalog"."sqlmesh__project"."project__test_model__684351896"`
0 commit comments