sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.43k stars 436 forks source link

Handle schema-qualified names in derive #627

Open blankhart opened 4 years ago

blankhart commented 4 years ago

The derived FromSql instance appears to fail when translating a Rust type to a Postgres type qualified by a schema name.

For example, this works fine:

  #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, FromSql, ToSql)]
  #[postgres(name = "service")]
  pub enum Service {
    Facebook,
    Google,
    Twitter,
  }

But this does not, the only difference being the "users." schema prefix:

  #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, FromSql, ToSql)]
  #[postgres(name = "users.service")]
  pub enum Service {
    Facebook,
    Google,
    Twitter,
  }

The Postgres types in each case would correspond, i.e.:

CREATE TYPE [users.]service AS ENUM (
  'Facebook',
  'Google',
  'Twitter'
);

Perhaps though there is a way to use the qualified Postgres type that I've overlooked?

Thank you for this wonderful library.

sfackler commented 4 years ago

The derive currently just ignores the schema of the type entirely I believe. Does the first code sample (with no schema qualification) not work with the type defined in a non-default schema?

blankhart commented 4 years ago

That works, yes, thanks! The behavior is perhaps confusing if the schema is accepted in some cases but produces errors in others. For example, pg_mapper accepts the schema. If it's too much to have consistency across the related packages it may be helpful to add a note to the docs for #[postgres].

sfackler commented 4 years ago

Yeah I think it is a bit weird that the logic currently ignores the schema entirely. I think it'd definitely make sense to handle the case where the type name is schema-qualified and add a check for the schema name at that point. Would you be interested in making a PR?

blankhart commented 4 years ago

Yes but I am new to Rust and databases and quite time constrained to if this is waiting on me it may be a while.

sfackler commented 4 years ago

No worries!