- Notifications
You must be signed in to change notification settings - Fork975
[Question] How to differentiate between fields of the same name coming from different tables in a JOIN ?#2712
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Consider the schema: CREATETABLE `Account` (`Id`bigint(20) unsignedNOT NULL AUTO_INCREMENT,`Relation`bigint(20) unsigned DEFAULTNULL,`Company`varchar(50) DEFAULTNULL,`MainAddressId`bigint(20) unsignednot null,`AlternateAddressId`bigint(20) defaultnull,`CreateDate` datetime DEFAULTCURRENT_TIMESTAMP,`ModifyDate` datetime DEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,PRIMARY KEY (`Id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;CREATETABLE `Locations` (`Id`bigint(20) unsignedNOT NULL AUTO_INCREMENT,`Address`VARCHAR(200)NOT NULL,`City`VARCHAR(80) defaultnull,`CreateDate` datetime DEFAULTCURRENT_TIMESTAMP,`ModifyDate` datetime DEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,PRIMARY KEY (`Id`)); and queries -- name: FindAccountById :oneSELECTsqlc.embed(a),sqlc.embed(locMain),sqlc.embed(c)FROM Account aLEFT JOIN Locations locMainONa.MainAddressId=locMain.IdLEFT JOIN Locations locAltONa.AlternateAddressId=locAlt.IdWHEREa.Id= ?LIMIT1; when generating the code (mysql driver), the generated method I'm not sure if there's something that can be currently done to easily differentiate or have the generated code use table aliases to tell the tables apart (so that the resulting struct elements would be called EDIT: rename:spotify_url:"SpotifyURL" doesn't seem useful in this case. The name of the resulting struct field depends on context (eg: every table may have an ID, but whether it ends up being Id_2 or Id_3 depends on the exact query and what joins happen). EDIT 2: EDIT 3: https://play.sqlc.dev/p/37c12941f7f1943997f4a628b82fb6be33d852a71614bb581a3ee14d71c280eb - initial version, fields mangled https://play.sqlc.dev/p/8aa8bb4478ca2f7a54533ec2d66a167e580a93f6879ab6af4c5ead6e6b6e989e - trying embed, but can't resolve https://play.sqlc.dev/p/31f018cd6e834cd2ccd9122789cd91c8adf29e7ea5e478e6a29dfafd2846903e - this is weird, it seems to resolve but the resulting embeds are numbered, so rendering them still confusing (as one would have to remember the order of the joins) Questions:
|
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Regarding your first question, embeds do resolve table aliases of any length, but there appears to be a bug related to letter casing. I've created an issue here:#2745. Here's your second playground link but with all alias names lower-cased, which works fine:https://play.sqlc.dev/p/e3de092a32a1c9b611322b42fc24ec1fabd2b3f751e357fac5e1f3d9342838ca But regarding your larger question about better names for the embedded struct fields, I think using the alias as part of the name (or actually the whole name?) is a good suggestion. It would be a breaking change, but we could put it behind a configuration flag. I think just using the alias name if provided is the right answer, so in this case that would generate a struct like: typeFindAccountByIdRowstruct {AAccountLocMainLocationLocAltLocation} |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hi - I also have this issue and was wondering if there is an issue open for supporting the alias in sqlc.embed(alias) as the field name of the generated struct fields? Edit: okay I found#3177 |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #2710 on September 07, 2023 17:11.