has_create_action?/1 excludes manual creates, making the #49 nullability fix inert on AshEvents resources

Open
#851 9 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Mckay-Hinds is already working on this.

Since Oct 28, 2026.

  • #913 by @hoanle0126 — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
elixir, postgresql
Domain
databases

Research direction

Start in migration_generator.ex at has_create_action?/1 and follow its count into merge_attributes/3. Reproduce the shared-table case with AshEvents-wrapped create actions; done when manual creates are counted and subtype-specific columns generate as nullable, preserving the behavior described for non-evented resources.

Written by the indexing model from the issue text.

Description

Version

ash_postgres 2.13.0, ash 3.32.3, ash_events in use on the affected resources.

Summary

has_create_action?/1 excludes actions that declare manual. AshEvents implements its audited actions as manual actions, so on any resource with an event log has_create_action? returns false even when the resource has ordinary create actions.

The consequence is that the fix for #49 — "required attributes that aren't on all resources using that table should not be marked as null: false" — is silently inert whenever the resources sharing a table are evented.

The chain

migration_generator.ex:

  defp has_create_action?(resource) do
    resource
    |> Ash.Resource.Info.actions()
    |> Enum.any?(&(&1.type == :create && !&1.manual))
  end

feeds the per-table count:

      count_with_create =
        snapshots
        |> Enum.count(& &1.has_create_action)

which is the count in merge_attributes/3:

        allow_nil?: Enum.any?(attributes, & &1.allow_nil?) || Enum.count(attributes) < count,

That second clause is #49's fix. When every resource on the table is evented, count is 0, so the clause is n < 0 and never fires.

Why manual is set

An AshEvents-wrapped create reports:

manual: {AshEvents.CreateActionWrapper, [action: :identify, event_log: MyApp.EventLog, version: 1, ...]}

so !&1.manual is false.

Reproduction

Two resources sharing one table, in the shape used for single-table inheritance — a supertype with no create actions, and subtypes each with a base_filter and their own columns:

# table "notes", resources: Note (supertype), TextNote, FlagNote

defmodule MyApp.TextNote do
  use Ash.Resource,
    domain: MyApp.Domain,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshEvents.Events]          # <- remove this and the bug disappears

  postgres do
    table "notes"
    repo MyApp.Repo
    base_filter_sql "kind = 'text'"
  end

  resource do
    base_filter expr(kind == :text)
  end

  events do
    event_log MyApp.EventLog
  end

  actions do
    defaults [:read]
    create :write do
      accept [:body]
    end
  end

  attributes do
    attribute :kind, :atom, allow_nil?: false, writable?: false, default: :text
    attribute :body, :string, allow_nil?: false     # declared only by this subtype
  end
end

FlagNote is the same shape with kind = 'flag' and its own :reason column.

Expected: body and reason are generated nullable — neither is declared by every creating resource on the table, which is exactly what #49 asks for.

Actual: both are generated null: false, so a row of either kind is impossible to insert:

    alter table(:notes) do
      add :body, :text, null: false
      add :reason, :text, null: false
    end

Removing extensions: [AshEvents.Events] from the subtypes makes has_create_action? return true and the columns generate nullable as expected.

Observed in a real app

Across one codebase, with has_create_action? evaluated per resource:

  Commitment          evented=false creates=0 -> has_create_action=false
  Appointment         evented=false creates=1 -> has_create_action=true
  Job                 evented=true  creates=2 -> has_create_action=false

Job has two ordinary create actions and reports false solely because they are wrapped.

Suggested fix

The predicate is about "does anything insert rows into this table", and a manual create does insert rows. Counting manual creates — Enum.any?(&(&1.type == :create)) — would restore #49's behaviour for evented resources. If manual actions are excluded deliberately for some other reason, it may be worth naming that reason in a comment, since the interaction with ash_events is invisible at the call site.

Workaround

Declare the kind-specific column allow_nil? true and enforce presence with a validation plus a check constraint. That works, but it means the resource no longer states its own rule.

Dominant language
Elixir
Stars
191
Forks
186
Avg merge
16h 4m
Merged PRs (30d)
17

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ash-project/ash_postgres

All issues in ash-project/ash_postgres

Similar issues

More Elixir issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.