jetify-com / typeid-sql

SQL implementation TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
Apache License 2.0
80 stars 6 forks source link

Rails/ActiveRecord/Postgres #3

Open kornate opened 1 year ago

kornate commented 1 year ago

Has anyone managed to get typeid-sql working with rails/activerecord/postgres?

Despite hours and hours of trying, I cannot get past this error:

PG::UndefinedFunction: ERROR:  operator is only a shell: character varying = typeid
LINE 1: ...rnal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT...
divaltor commented 1 year ago

I've met the same issue

Try to remove COMMUTATOR from operator overloading

CREATE OR REPLACE FUNCTION compare_type_id_equality(lhs_id typeid, rhs_id VARCHAR)
    RETURNS BOOLEAN AS $$
SELECT lhs_id = typeid_parse(rhs_id);
$$ LANGUAGE SQL IMMUTABLE;

CREATE OPERATOR = (
    LEFTARG = typeid,
    RIGHTARG = varchar,
    FUNCTION = compare_type_id_equality,
    NEGATOR = !=,
    HASHES,
    MERGES
);

When you provide COMMUTATOR parameter - PostgreSQL creates 2 operators for = - (typeid, varchar) and (varchar, typeid) and when on left side you have varchar value PostgreSQL try to execute function for validating typeid on right side even if you have no typeid type there

With COMMUTATOR

image

Without COMMUTATOR

image