Using Enums in a Query #373
-
Here is a the SQL I'm trying to generate for my table. SELECT buckets.bucket_id AS "getbucketpolicybyenvrow.bucket_id",
bucket_policy.policy_id AS "getbucketpolicybyenvrow.policy_id",
buckets.environment_name AS "getbucketpolicybyenvrow.environment_name",
buckets.create_bucket AS "getbucketpolicybyenvrow.create_bucket",
buckets.bucket_name AS "getbucketpolicybyenvrow.bucket_name",
bucket_policy.policy_name AS "getbucketpolicybyenvrow.policy_name",
bucket_policy.duration AS "getbucketpolicybyenvrow.duration",
bucket_policy.prefix AS "getbucketpolicybyenvrow.prefix"
FROM storage.buckets
INNER JOIN storage.bucket_policy ON (bucket_policy.bucket_id = buckets.bucket_id)
WHERE buckets.environment_name = 'production'; The code I am trying to use to mimic this pattern is below. (My struct is named GetBucketPolicyByEnvRow hence the prefix in the alias, if anyone knows how to avoid that it would be great as well)
The issue is that the environment_name is an enum, and the code that is generated has the following where condition: WHERE buckets.environment_name = $1::text which is invalid, as now we are comparing an enum to a string. The table definition has environmentName as a string:
while the model is a valid enum data type.
I found a work around where I use this instead in the Where clause: WHERE(RawBool("buckets.environment_name = $1::storage.environment", RawArgs{"$1": enumVal.String()})) Is this expected behavior, or am I doing something wrong and I should open up an issue? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you have enums in your schema, generator will generate WHERE(storageSchema.Buckets.EnvironmentName.EQ(enum.Enviroment.Production)) |
Beta Was this translation helpful? Give feedback.
If you have enums in your schema, generator will generate
enum
package for you. After importing the package, you can write: