ash-project/ash

Ash.Resource does not translate into Ecto.Schema with correct autogenerate

Geschlossen

#708 geöffnet am 27.09.2023

 (9 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Elixir (389 Forks)batch import
enhancementgood first issue

Repository-Metriken

Stars
 (2.412 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 6h) (26 gemergte PRs in 30 T)

Beschreibung

Describe the bug I am not sure if this is a bug or a feature seen from the project, but for me it is a bug.

To Reproduce

  • Create an ecto schema where some fields have defaults and autogenerate, like etc the timestamps
defmodule Shared.Config.Version do
  @moduledoc false

  schema "conf_version" do
    field(:version, :integer, default: 1)

    timestamps()
  end
end
  • Create a Ash.Resource where some attribute has defaults
defmodule Shared.Ash.Config.Version do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  attributes do
    uuid_primary_key :id

    attribute :version, :integer do
      default 1
    end

    update_timestamp :updated_at
    create_timestamp :inserted_at
  end

  postgres do
    table "conf_version"
    repo(Shared.Repo)
  end

  actions do
    defaults [:create, :read, :update, :destroy]
  end
end

Now try to insert each of them using Ecto Repo.insert! and only the Ecto schema is inserted with default values and the timestamps are autogenerated

One can also use MyAshResource.schema(:autogenerate_fields) to see the results of my question.

Expected behavior Expects the Ash.Resource to generate a Ecto.Schema that has the autogenerate options set

Shared.Config.Version.__schema__(:autogenerate_fields) #=> [:inserted_at, :updated_at]

Shared.Ash.Config.Version.__schema__(:autogenerate_fields) #=> []
%Shared.Config.Version{} |> dbg()

%Shared.Config.Version{} #=> %Shared.Config.Version{
  __meta__: #Ecto.Schema.Metadata<:built, "conf_version">,
  id: nil,
  version: 1,
  inserted_at: nil,
  updated_at: nil
}


%Shared.Ash.Config.Version{} |> dbg()

%Shared.Ash.Config.Version{} #=> #Shared.Ash.Config.Version<
  node: #Ash.NotLoaded<:relationship>,
  __meta__: #Ecto.Schema.Metadata<:built, "conf_version">,
  id: nil,
  version: nil,
  updated_at: nil,
  inserted_at: nil,
  aggregates: %{},
  calculations: %{},
  ...
>


Contributor Guide