Troubleshooting mix ash.codegen creates wrong foreign key type

Hi,

in all my Ash.Resources I define the primary key as follows

attributes do
  integer_primary_key(:id)
  attribute ...

If I run mix ash.codegen <some_name> all migrations where build, but with a uuid foreign key for all defined relations.

Is this a default? Do I have to define the type of the foreign key myself?

Thanks

Hi @ronscho,

Could you please clarify:

  1. The type of the primary key on the related resources, and
  2. How relationships are defined?

Please share the code if possible.

Relationships — ash v3.4.47 due to compilation constraints, we cannot guess the type of foreign keys based on the types of the destination attributes. It would cause potential compiler deadlocks. Instead, it just defaults to uuid, but you can change that default as shown in the section titled " Customizing default belongs_to attribute type" in the docs I linked.

1 Like

Hi,

yes I can share an example.

defmodule Project.Domain.Foo do
  use Ash.Resource,
    domain: Project.Domain,
    data_layer: AshPostgres.DataLayer

  postgres do
    table("foos")
    repo(Project.Repo)
  end

  attributes do
    integer_primary_key(:id)
    attribute(:name, :string)

    timestamps()
  end

   relationships do
     belongs_to(:bar, Project.Domain.Bar) 
   end
end

defmodule Project.Domain.Bar do
  use Ash.Resource,
    domain: Project.Domain,
    data_layer: AshPostgres.DataLayer


  postgres do
    table("bar")
    repo(Project.Repo)
  end

  attributes do
    integer_primary_key(:id)
    attribute(:name, :string)
  end

  relationships do
    has_many(:foo, Project.Domain.Foo)
  end
end

I’m not allowed to share the actual code.

I hope this example will clear things up :slight_smile:

Thank you, it’s working perfectly.

2 Likes