I have a schema (“customers”) with a has_many association (“subscriptions”), and my issue is that when the “customers” record is created, the has_many “subscriptions” association will always be an empty enum. This is fine when inserting, but results in a ** (UndefinedFunctionError) function Mongo.Ecto.in_transaction?/1 is undefined or private error when retrieving the record from the DB.
Any ideas how to resolve this?
Customers Schema
schema "customers" do
field :name, :string
field :phone, :string
field :email, :string
field :stripe_id, :string
field :description, :string
field :balance, :float
field :currency, :string
field :discount, :float
field :payment_method, :string
field :deleted_from_stripe, :boolean
has_one :address, Address
has_many :subscriptions, Subscription
timestamps()
end
Customers Changeset
def changeset(customer, params) do
customer
|> cast(params, @attrs)
|> cast_assoc(:address, with: &Address.changeset/2)
|> cast_assoc(:subscriptions, with: &Subscription.changeset/2)
end
Customers Query
def find_customers() do
query =
from t in Customer,
preload: [:address, :subscriptions],
select: t
Repo.all(query)
end
Based on the chatter in the elixir_mongo issues, it sounds like support for Ecto 2 wasn’t easy and required some pushback to the core Ecto library, for instance:
def application do
[
mod: {MyApp.Application, []},
extra_applications: [:logger, :mongodb_ecto, :ecto, :runtime_tools, :edeliver]
]
end
Repo
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Mongo.Ecto
end
Model
defmodule MyApp.Model do
defmacro __using__(_) do
quote do
use Ecto.Model
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id # For associations
end
end
end
Ok thanks for that, I’ve replaced Ecto.Model with Ecto.Schema and compilation is now fine, but the app is now failing to load the front end JS - looks like I’ve started down a rabbit hole
Indeed My only recommendation is to take it slow and not try to upgrade straight from 2.1 to 3.9, even though that’s going to mean making changes that are then undone/further changed later.
Oh I’m not trying to upgrade to 3.9, 2.1 is fine for me, don’t need anything special from Ecto, only basic CRUD operations.
Strange how this has impacted on loading my assets though, the only dependencies I’ve changed are phoenix_ecto (upgraded from 3.0 to 3.1), mongo_ecto (changed to mongodb_ecto)