terraform-provider-snowflake icon indicating copy to clipboard operation
terraform-provider-snowflake copied to clipboard

"The session does not have a current database" when creating external table

Open glittershark opened this issue 2 years ago • 1 comments

Provider Version

`0.34.0

Terraform Version

1.1.6

Describe the bug

I've got something that looks like the following, for querying JSON files stored in an s3 bucket:

resource "snowflake_storage_integration" "foo_s3" {
  name    = "FOO_S3"
  type    = "EXTERNAL_STAGE"
  enabled = true

  storage_provider = "S3"
  storage_allowed_locations = [
    "s3://${data.aws_s3_bucket.foo_bucket.bucket}"
  ]
  storage_aws_role_arn = aws_iam_role.snowflake_foo.arn
}

resource "snowflake_database" "foo" {
  name = "FOO"
}

resource "snowflake_stage" "foo_s3" {
  name                = "FOO_S3"
  url                 = "s3://${data.aws_s3_bucket.foo_bucket.bucket}"
  database            = snowflake_database.foo.name
  schema              = "PUBLIC"
  storage_integration = snowflake_storage_integration.foo_s3.name
}

resource "snowflake_external_table" "foo" {
  database    = snowflake_database.foo.name
  schema      = "PUBLIC"
  name        = "FOO"
  file_format = "type = json"
  location    = "@${snowflake_stage.foo_s3.name}"

  column {
    name = "id"
    type = "text"
    as   = "metadata$filename"
  }

  column {
    name = "data"
    type = "variant"
    as   = "value"
  }
}

When I run terraform apply, I get the following error message:

╷
│ Error: error creating externalTable FOO: 090105 (22000): Cannot perform STAGE GET. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.

I've tried replacing the location with @${snowflake_database.foo.name}.${snowflake_stage.foo_s3.name}", but that gave the same error message

Expected behavior

I'd like the external table to create successfully

glittershark avatar Jun 03 '22 15:06 glittershark

"@${snowflake_database.foo.name}.PUBLIC.${snowflake_stage.foo_s3.name}" fixed it. Maybe this could be documented?

glittershark avatar Jun 03 '22 15:06 glittershark