New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler error for DISTINCT ON that doesn't match initial ORDER BY expressions #1641

Open
connec opened this Issue Apr 14, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@connec

connec commented Apr 14, 2018

For example:

table! {
    entries (id) {
        id -> Int4,
        name -> Text,
        date -> Timestamp,
    }
}

// Attempt to get the latest entries for each `name`
entries
    .order_by(date.desc())
    .distinct_on(name)
    .load(&conn)

This compiles fine, but inevitably causes "SELECT DISTINCT ON expressions must match initial ORDER BY expressions" when executed.

The correct version, of course, is:

entries
    .order_by((name, date.desc()))
    .distinct_on(name)
    .load(&conn)

According to the PostgreSQL docs DISTINCT ON is valid with no ordering, or with a matching prefix. I don't know enough about Rust to have any idea how feasible it is to encode that in the types, but it would be a neat sanity check!

Checklist

  • I have already looked over the issue tracker for similar issues.

@connec connec changed the title from Compiler error for DISTINCT ON that doesn't match initial ORDER BY expressions to Add compiler error for DISTINCT ON that doesn't match initial ORDER BY expressions Apr 14, 2018

@sgrif

This comment has been minimized.

Member

sgrif commented Apr 14, 2018

I'm not sure that it's possible for us to encode this (without a breaking change), but we definitely should be

@sgrif sgrif added the bug label Apr 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment