Unsupported expression in Elixir.AshPostgres.SqlImplementation query: `datetime_add`

Can't quite tell what I'm doing wrong here. The OrgSubscription is a many_to_many resource that connects Organization to SubscriptionPlan.
defmodule OrgSubscription do
...
attribute :activation_date, :utc_datetime do
public? true
allow_nil? false
default &DateTime.utc_now/0
end
...
calculate :expiration_date,
:utc_datetime,
expr(
datetime_add(
activation_date,
subscription_plan.duration_qty,
subscription_plan.duration_interval
)
)
end
defmodule OrgSubscription do
...
attribute :activation_date, :utc_datetime do
public? true
allow_nil? false
default &DateTime.utc_now/0
end
...
calculate :expiration_date,
:utc_datetime,
expr(
datetime_add(
activation_date,
subscription_plan.duration_qty,
subscription_plan.duration_interval
)
)
end
In SubscriptionPlan, the duration_qty and duration_interval attributes are defined like so:
attribute :duration_interval, :atom do
allow_nil? false
public? true
constraints one_of: [:day, :week, :month, :year]
default :year
end

attribute :duration_qty, :integer do
allow_nil? false
public? true
constraints min: 1, max: 100
default 1
end
attribute :duration_interval, :atom do
allow_nil? false
public? true
constraints one_of: [:day, :week, :month, :year]
default :year
end

attribute :duration_qty, :integer do
allow_nil? false
public? true
constraints min: 1, max: 100
default 1
end
When I try to run the calculation, I get this error:
Unsupported expression in Elixir.AshPostgres.SqlImplementation query:

%{
extra: %{},
name: :datetime_add,
arguments: [
%{
attribute: %{
default: &DateTime.utc_now/0,
name: :activation_date,
type: Ash.Type.UtcDatetime,
constraints: [
precision: :second,
cast_dates_as: :start_of_day,
timezone: :utc
],
},
},
%{
attribute: %{
default: 1,
name: :duration_qty,
type: Ash.Type.Integer,
source: :duration_qty,
constraints: [min: 1, max: 100]
},
},
%{
attribute: %{
default: :year,
name: :duration_interval,
type: Ash.Type.Atom,
description: nil,
source: :duration_interval,
constraints: [
one_of: [:day, :week, :month, :year],
unsafe_to_atom?: false
],
},
}
],
}
Unsupported expression in Elixir.AshPostgres.SqlImplementation query:

%{
extra: %{},
name: :datetime_add,
arguments: [
%{
attribute: %{
default: &DateTime.utc_now/0,
name: :activation_date,
type: Ash.Type.UtcDatetime,
constraints: [
precision: :second,
cast_dates_as: :start_of_day,
timezone: :utc
],
},
},
%{
attribute: %{
default: 1,
name: :duration_qty,
type: Ash.Type.Integer,
source: :duration_qty,
constraints: [min: 1, max: 100]
},
},
%{
attribute: %{
default: :year,
name: :duration_interval,
type: Ash.Type.Atom,
description: nil,
source: :duration_interval,
constraints: [
one_of: [:day, :week, :month, :year],
unsafe_to_atom?: false
],
},
}
],
}
Solution:
You may need to use a fragment
Jump to solution
3 Replies
ZachDaniel
ZachDaniel4mo ago
Hmm...I don't think that expression supports references as the amount/duration? Or maybe it's a bug there?
Solution
ZachDaniel
ZachDaniel4mo ago
You may need to use a fragment
⿻ eileen
⿻ eileenOP4mo ago
Fragment worked! Claude did me a solid:
expr(
fragment(
"? + (? || ' ' || ?)::interval",
activation_date,
subscription_plan.duration_qty,
subscription_plan.duration_interval
)
)
expr(
fragment(
"? + (? || ' ' || ?)::interval",
activation_date,
subscription_plan.duration_qty,
subscription_plan.duration_interval
)
)

Did you find this page helpful?