ash-project / ash_postgres

The PostgreSQL data layer for Ash Framework
https://hexdocs.pm/ash_postgres
MIT License
134 stars 73 forks source link

Ash.Type.NewType: A type defined as subtype_of: :ci_string leads to postgres error in read actions. Attribute is cast as ::ci_string instead of ::citext #375

Closed heinij closed 2 weeks ago

heinij commented 2 weeks ago

Describe the bug Defining a new type with

  use Ash.Type.NewType,
    subtype_of: :ci_string,

and using the new type as the type of an attribute leads to postgres error in read actions Postgres casts the attribute as ::ci_string instead of ::citext

Used for an action argument it is cast correctly as ::citext

To Reproduce

defmodule MyApp.Types.Prefix do
  use Ash.Type.NewType,
    subtype_of: :ci_string,
    constraints: [casing: :upper]
end

defmodule MyApp.Orgs.Org do
...
  attributes do
    ...
    attribute :prefix, MyApp.Types.Prefix, allow_nil?: false 
  end

  actions do
    read :by_prefix do
      argument :prefix, MyApp.Types.Prefix
      filter(expr(prefix==^arg(:prefix)))
    end
  end

Calling MyApp.Orgs.Org.by_prefix("ABCD") gives posgres error:

%Ash.Error.Unknown.UnknownError{
  error: "** (Postgrex.Error) ERROR 42704 (undefined_object) 
  type \"ci_string\" does not exist\n\n    
  query: SELECT o0.\"id\", o0.\"prefix\"  FROM \"orgs\" AS o0 WHERE (o0.\"prefix\"::ci_string = ($1::citext))",

Runtime

zachdaniel commented 2 weeks ago

Fixed in main :)