sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.37k stars 803 forks source link

Invalid "Column is ambiguous error" #1448

Closed Davincible closed 1 year ago

Davincible commented 2 years ago

Version

1.12.0

What happened?

SELECT
    "UserRecords"."Username"
FROM
    "Monitor"
    JOIN ( SELECT DISTINCT ON ("User")
            *
        FROM
            "UserRecords"
        ORDER BY
            "User",
            "CreatedAt" DESC) "UserRecords" ON "Monitor"."ID" = "UserRecords"."User"
WHERE
    "Monitor"."Monitor" = TRUE;

Gives column is ambiguous. This happens because I have a table called "UserRecords", as well as called the JOIN table "UserRecords". However the SQL is not actually ambiguous and executes fine, sqlc confuses the two when there is no reason to. Renaming the JOIN table mitigates it for now.

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

No response

What database engines are you using?

No response

What type of code are you generating?

No response

Davincible commented 2 years ago

Another issue for the same query, the star expansion does not seem to work here properly unfortunately. The following query gets generated:

SELECT
    "Monitor"."ID",
    "UserRecordsU"."Username"
FROM
    "Monitor"
    JOIN ( SELECT DISTINCT ON ("User")
            ID, "User", CreatedAt, Username, FullName, Biography, FollowingCount, FollowerCount, IsBusiness, IsPrivate, IsVerified, HasVideos, Category, ClipsCount, IsPotentialBusiness, HasHighlightReels, MediaCount, HasGuides, GeoMediaCount, CTAEnabled, FollowingTagCount, BiographyEntities, BiographyProductMentions, ExternalUrl, IGTVVideos, HasIGTVSeries, ProfilePicture, AREffects, UsertagsCount, ShowShoppableFeed, ShoppablePosts, CanBeReported, MerchantChecoutStyle, SellerShoppableFeedType, VisibleProducts, HasAffiliateShop, CreatorShoppingInfo, IsEligibleForSMBSupportFlow, SMBSupportPartner, DirectMessaging, AddressStreet, BusinessContactMethod, CityID, CityName, ContactPhoneNumber, Latitude, Longitude, PublicEmail, PublicPhoneCountryCode, PublicPhoneNumber, Zip, SuggestedBusinessAccType, AccountType, FundraiserInfo, AccountBadges, IncludeDirectBlacklistStatus
        FROM
            "UserRecords"
        ORDER BY
            "User",
            "CreatedAt" DESC) "UserRecordsU" ON "Monitor"."ID" = "UserRecordsU"."User"
WHERE
    "Monitor"."Monitor" = TRUE

And here only the User column gets quoted, where all columns should be quoted. Normally that works perfectly, but for this one query it seems to break.

kyleconroy commented 2 years ago

@Davincible Could you please provide the schema for the Monitor and User tables?