Ecto fragment as a query source?

I have the same issue with @sztosz were i wanted to use TABLESAMPLE system_rows(n) instead of RANDOM() and thank you to @mbuhot on his answer, with a little tweak, as I wanted to get the result as an Ecto query:

"img"
|> with_cte("img", as: fragment("select * from images TABLESAMPLE system_rows(1)"))
|> from(as: ^:img)
|> select([img: i],
	%Images{
    title: i.title,
    description: i.description,
    slug: i.slug,
    tags: i.tags,
	}
)
|> limit(1)
|> Repo.paginate()
2 Likes