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

Be more generic over tables #537

Closed
jethrogb opened this Issue Dec 9, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@jethrogb
Contributor

jethrogb commented Dec 9, 2016

I have multiple tables that have the exact same schema. I'd like to be able to write code that's generic over such tables. I'm running into some issues:

  • Queryable - seems fine
  • Insertable - can only specify one table when using derive() but could technically be implemented for multiple tables
  • Identifiable - has an associated type pointing at a particular table
  • AsChangeset - has an associated type pointing at a particular table
@sgrif

This comment has been minimized.

Member

sgrif commented Dec 16, 2017

We may add support for multiple tables in the future, but for the time being the best way to handle this is going to be to implement Insertable by hand.

Example implementation:

struct SomeForm {
    field1: String,
    field2: String,
}

impl<'a> Insertable<table1::table> for &'a SomeForm {
    type Values = <(Eq<table1::field1, &'a str>, Eq<table1::field2, &'a str>) as Insertable<table1::table>>::Values;

    fn values(&self) -> Self::Values {
        (table1::field1.eq(&self.field1), table2::field2.eq(&self.field2)).values()
    }
}

I think you're right that AsChangeset could use a type parameter instead of an associated type, but at this point we can't make a breaking change there. You could probably use a macro + tuples for this case instead. Identifiable would never be used with more than one table, since its point is literally "this struct represents a single row on a single table", and one of its items is "what table is this associated with?

@sgrif sgrif closed this Dec 16, 2017

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