Search code examples
postgresqlrustcode-generationcornucopia

How to handle Postgres HStore type with Rust Cornucopia


When using Cornucopia to generate Rust structs from a managed Postgres database I get the following error:

this query contains an unsupported type (name: identities, type: hstore)

We have a few rows like this in our database and it would be great to not have to return everything as json and manually parse since that would defeat the purpose of auto-generating types in the first place.

Here is and example SQL query:

SELECT * FROM users;

and here is the generation command:

cornucopia --serialize live $DATABASE_URL

Solution

  • I was able to work around this issue for the time being by casting to json in the SQL statement

    SELECT 
        username, 
        registered_at::varchar,
        created_at::varchar,
        COALESCE(hstore_to_json(identities), '{}'::json) AS identities,
        email, 
        id 
    FROM users;
    

    it's a bit more verbose, but at least it's working!