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

Joining on arbitrary string field #838

Closed
dbrgn opened this Issue Apr 4, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@dbrgn
Contributor

dbrgn commented Apr 4, 2017

I have a table with user information and a table with a public key cache.

I'd like to join those on the user id field. There is no foreign key on the tables though, since a cached key could exist without a user, and a user could exist without a cached key.

Is there a way to join two tables on an arbitrary field name, using either the string builder or a raw query?

I tried a raw query, but didn't get it to work:

let query = sql::<(recipients::SqlType, pubkey_cache::SqlType)>(
    &format!("SELECT r.*, c.*
              FROM recipients r
              INNER JOIN pubkey_cache c ON r.user_id = c.user_id
              WHERE r.broadcast_id = {}", broadcast.id)
);
let recipients = query.get_results::<(recipients::SqlType, pubkey_cache::SqlType)>(conn)?;

Any pointers are warmly appreciated. I already searched through the test suite and through the issue tracker, but didn't find a working solution.

@sgrif

This comment has been minimized.

Member

sgrif commented Apr 4, 2017

I mentioned it in gitter, but your issue is that you're passing the wrong type to get_results. It should be the type you want to deserialize to. We already know the SQL type from the query itself.

@sgrif sgrif closed this Apr 4, 2017

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