1

I created a DOMAIN as following:

CREATE DOMAIN country_id AS uuid;

I create such domains for readability. It is much easier to read function definitions which accept a country_id instead of a uuid.

So, continuing, I have a function which accepts a country_id:

CREATE FUNCTION list_cities(id country_id) returns SET OF city AS
$$ ... $$ language sql stable;

When I have a text representing a country identifier (for example coming from JSON parsing), I need to manually add a cast as following:

SELECT list_cities((some_json->>'id')::country_id);

Is it possible to define a cast which eliminates the need to add the manual ::country_id cast each time? It would be much cleaner if I could do instead:

SELECT list_cities(some_json->>'id');

Another example could be replacing the following query:

SELECT * FROM country WHERE id = (some_json->>'id')::country_id

by:

SELECT * FROM country WHERE id = some_json->>'id'

Is it impossible to avoid such manual casts by defining an implicit cast from text to country_id/uuid?

  • You could create a second function which takes a varchar Argument and calls the first one with the cast. – eckes Sep 20 '18 at 11:33
  • That's a good suggestion, but the function was just an example. Another example could be replacing SELECT * FROM country WHERE id = (some_json->>'id')::country_id by SELECT * FROM country WHERE id = some_json->>'id'. I'm wondering if 'any' kind of cast could be avoided by defining an implicit cast, but haven't been able to figure it out so far. – Katrin Sep 20 '18 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.