Unable to create duplicate Bigquery Tables and Views in separate datasets

I am trying to create same Bigquery tables and views in separate datasets but it is giving me error like

Error Duplicate object key
view = { for view in var.views : view[“view_id”] => view}
view[“view_id”] is bar1
Two different items produced the key “bar1” in this for expression. if duplicates are expected, use the ellipsis (…) after the value expression to enable grouping by key.

Here is the main module

locals {
tables = { for table in var.tables : table[“table_id”] => table }
views = { for view in var.views : view[“view_id”] => view }
}
resource “google_bigquery_table” “view” {
for_each = local.views
dataset_id = google_bigquery_dataset.main.dataset_id
friendly_name = each.key
table_id = each.key
labels = each.value[“labels”]
project = var.project_id
view {
query = file(each.value[“query”])
use_legacy_sql = false
}
}

Here is the code which I am using to create views.

views = [
{
view_id = “bar1”,
dataset_id = “PII”,
query = “bar.sql”
},
{
view_id = “bar1”,
dataset_id = “NOPII”,
query = “bar.sql”
}
]

This is the base terraform templates I am referring

1 Like