Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Dataform Declaration Aliases

Say you are trying to declare some data sources, but you have an ugly table name. E.g:

 

 

 

config {
    type: "declaration",
    database: "myDatabase",
    schema: "dbo",
    name: "main_partitioned_on_date_clustered_on_last_name",
}

 

Is there a way to alias this for when I call it as a ref?

For example, being able to call it as '${ref("dbo_main")}' or something. I have tried changing the file name but that did not work.

Solved Solved
1 8 4,654
1 ACCEPTED SOLUTION

In Dataform, you can indeed create aliases for tables to make them easier to reference in your scripts. While the config block is used to declare the table, you can assign an alias to this table that can be used throughout your Dataform scripts.

Here's how you can do it:

  1. Declare the Table with a Config Block: You've already done this part. You declare your table with its full name in the config block.

  2. Assign an Alias: After declaring the table, you can assign an alias to it. This is done using the publish function. The alias is the name you give to the publish function.

Here's an example:

 
publish("short_name") {
  description: "An alias for the long table name"
  config {
    type: "view"
    database: "myDatabase"
    schema: "dbo"
    name: "main_partitioned_on_date_clustered_on_last_name"
  }
  query: `SELECT * FROM ${ref("main_partitioned_on_date_clustered_on_last_name")}`
}

In this example, "short_name" is the alias for your long table name. Whenever you want to reference this table in other scripts, you can use ref("short_name") instead of the full table name.

This approach makes your scripts cleaner and easier to read, especially when dealing with tables that have long or complex names.

View solution in original post

8 REPLIES 8