Skip to content

Problem with queries and schemas after v0.3.0 update #70

@mcraveiro

Description

@mcraveiro

Hi sqlgen developers,

I just updated my vcpkg to latest, picking up v0.3.0 [1]. I am experiencing an issue querying existing tables. The behaviour is as follows. I have an entity:

struct currency_entity {
    constexpr static const char* schema = "oresdb";
    constexpr static const char* tablename = "currencies";

    sqlgen::PrimaryKey<std::string> iso_code;
    std::string name;
    std::string numeric_code;
    std::string symbol;
    std::string fraction_symbol;
    int fractions_per_unit;
    std::string rounding_type;
    int rounding_precision;
    std::string format;
    std::string currency_type;
    std::string modified_by;
    sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S"> valid_from = "9999-12-31 23:59:59";
    sqlgen::Timestamp<"%Y-%m-%d %H:%M:%S"> valid_to = "9999-12-31 23:59:59";
};

It is queried like so:

std::vector<domain::currency> currency_repository::read_latest(context ctx) {
    BOOST_LOG_SEV(lg, debug) << "Reading latest currencies.";

    static auto max(make_timestamp(max_timestamp));
    const auto query = sqlgen::read<std::vector<currency_entity>> |
        where("valid_to"_c == max.value()) |
        order_by("valid_from"_c.desc());

    const auto sql = postgres::to_sql(query);
    BOOST_LOG_SEV(lg, debug) << "Query: " << sql;

    const auto r = session(ctx.connection_pool())
        .and_then(query);
    ensure_success(r);
    BOOST_LOG_SEV(lg, debug) << "Read latest currencies. Total: " << r->size();
    return currency_mapper::map(*r);
}

In version 0.2.0 the generated SQL was as follows:

SELECT "iso_code", "name", "numeric_code", "symbol", "fraction_symbol", "fractions_per_unit", "rounding_type", "rounding_precision", "format", "currency_type", "modified_by", "valid_from", "valid_to" FROM "oresdb"."currencies" WHERE "valid_to" = '9999-12-31 23:59:59' ORDER BY "valid_from" DESC

Please note the FROM (i.e., FROM "oresdb"."currencies"). After pulling v0.3.0, I now see the following:

SELECT "iso_code", "name", "numeric_code", "symbol", "fraction_symbol", "fractions_per_unit", "rounding_type", "rounding_precision", "format", "currency_type", "modified_by", "valid_from", "valid_to" FROM "currencies" WHERE "valid_to" = '9999-12-31 23:59:59' ORDER BY "valid_from" DESC

The schema appears to now be absent in the from (i.e., FROM "currencies"). This then results in a postgres error:

2025-10-22 14:46:16.092334 [ERROR] [ores.risk.repository.currency_repository] Executing 'DECLARE sqlgen_cursor CURSOR FOR SELECT "iso_code", "name", "numeric_code", "symbol", "fraction_symbol", "fractions_per_unit", "rounding_type", "rounding_precision", "format", "currency_type", "modified_by", "valid_from", "valid_to" FROM "currencies" WHERE "valid_to" = '9999-12-31 23:59:59' ORDER BY "valid_from" DESC' failed: ERROR:  relation "currencies" does not exist
LINE 1: ...pe", "modified_by", "valid_from", "valid_to" FROM "currencie...

I had a brief layperson's look at a diff between versions - I could not make much of the diff to be fair :-) but I did notice some changes around select:

Image

Having said that, to the untrained eye the new code seems to be taking into account the schema name so I am probably barking at the wrong tree:

Image

At any rate, any pointers as to why I am experiencing this would be most appreciated.

Thanks for all your hard work.

[1] https://vcpkg.io/en/package/sqlgen

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions