Arsenalist

Arsenalist

I’m sorting by a join field and it’s throwing an error. Here’s the query being executed:

    from(p in Product,
      left_join: variants in assoc(p, :variants), as: :variants,
      left_join: venue_arrangement in assoc(p, :venue_arrangement)
      preload: [
        variants: variants
      ]
    )

Here are the schemas:

defmodule Amplify.Models.Product do
  use Amplify.MedusaSchema

  @derive {
    Flop.Schema,
    filterable: [:showtimes],
    sortable: [:showtimes],
    adapter_opts: [
      join_fields: [
        showtimes: [
          binding: :variants,
          field: :starts_at_utc,
          path: [:variants, :utc_datetime]
        ]
      ]
    ]
  }

  schema "product" do
    field :title, :string
    has_many :variants, Amplify.Models.ProductVariant
  end
end

defmodule Amplify.Models.ProductVariant do
  use Amplify.MedusaSchema

  schema "product_variant" do
    belongs_to :product, Amplify.Models.Product
    field :thumbnail, :string
    field :starts_at_utc, :utc_datetime
  end
end

When I pass in ["showtimes"] as order_directions to flop params and call like this:

Flop.validate_and_run(query, flop_params)

I get:

** (Postgrex.Error) ERROR 42703 (undefined_column) column p0.showtimes does not exist

Showing Posts 1 to 7

woylie

woylie

You’ll need to pass the schema module. Flop doesn’t infer the schema automatically.

Flop.validate_and_run(query, flop_params, for: Amplify.Models.Product)
woylie

woylie

Unrelated, the path option is used to find the cursor values from the result set in case you use cursor pagination. This should thus be [:variants, :starts_at_utc].

Arsenalist

Arsenalist OP

Amazing, thank you. That worked perfectly. This library is eliminating the need to write a ton of code for me. Thank you again!

Arsenalist

Arsenalist OP

Hi, somewhat related, but do you know how I can print the underlying query that Flop creates when appending the LIMIT etc clauses? I know how to print a plain old Ecto query but how do I access the one that Flop creates when running validate_and_run? In dev this automatically prints but I’m trying to debug something i production.

Arsenalist

Arsenalist OP

I find that the LIMIT is not being appended to the query (the total count still returns the right count though).

    {:ok, {results, meta}} = Flop.validate_and_run(query, %{
      "first" => 20,
      "order_by" => ["showtimes"],
      "order_directions" => ["asc"]
    }, for: Amplify.Models.Product)
    meta = Map.put(meta, :total_count, Flop.count(query, meta.flop))
    %{metadata: meta, data: results}

I have a fairly complex query with about 12 joins, but that should’t matter?

This also appears to work perfectly fine in development, but in production, the limit is not being applied

Arsenalist

Arsenalist OP

Did some digging around and was able to print the query. The query is printed correctly with the right LIMITs applied and everything. When I run the query directly against the DB it works great.

However, Flop doesn’t return all the results from validate_and_run/3. This is only a problem when there’s a query being passed in. It works fine when you’re just querying a model directly (without joins etc.)

Arsenalist

Arsenalist OP

So the issue here was that I wasn’t adding distinct: p.id in my query. Once I added that it all behaved properly. I’m not sure why that matters since Flop should just return whatever it finds. As an example, here’s what I was seeing:

    {:ok, {results, meta}} = Flop.validate_and_run(query, %{
      first: 20,
      order_by: ["showtimes"],
      order_directions: ["asc"]
    }, for: Amplify.Models.Product)
    IO.inspect "total length is #{length(results)}". # this prints 1
    meta = Map.put(meta, :total_count, Flop.count(query, meta.flop))
    IO.inspect "total count is #{meta.total_count}" # this prints 154
    %{metadata: meta, data: results}
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews