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

infer_schema! errors when column is the same name with the table it contains #624

Closed
ivanceras opened this Issue Feb 5, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@ivanceras
Contributor

ivanceras commented Feb 5, 2017

Table which has a column name same to the table such as

CREATE TABLE message(
id serial PRIMARY KEY NOT NULL,
message character varying
);

will error with:

error[E0252]: a type named `message` has already been imported in this module
 --> src/schema.rs:3:1
  |
3 | infer_schema!("dotenv:DATABASE_URL");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | previous import of `message` here
  | already imported

A workaround is to simply not use a column with the same name as the table, such as

CREATE TABLE message(
id serial PRIMARY KEY NOT NULL,
body character varying
);

This is a minor issue, but the rust error message is a bit misleading.

@ivanceras ivanceras changed the title from Error when column is the same name with the table it contains to infer_schema! errors when column is the same name with the table it contains Feb 5, 2017

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 8, 2017

I think the specific source of this error is likely the dsl module.

mod $table_name {
    pub struct table;

    mod columns {
        // ...
    }

    mod dsl {
        pub use super::table as $table_name;
        pub use super::columns::*;
    }
}

In theory we could just skip generating the dsl module if there's a column with the same name as the table. That is potentially even more confusing than an error though. If nothing else, giving a more helpful error message is certainly the easiest thing to do here.

@killercup

This comment has been minimized.

Member

killercup commented Sep 1, 2017

Hm. To solve this, you'll either need to

  • adjust the table! macro to find—at expansion time—column names that equal the table name (not even sure if you can encode that as macro), or
  • adjust the schema inference (codegen) to return an error there (or maybe rename the column).

notryanb added a commit to notryanb/diesel that referenced this issue Sep 18, 2017

Generate compile time error for table! double import
Generates a better error message when a column
shares the same name as its table.

Fixes diesel-rs#624

notryanb added a commit to notryanb/diesel that referenced this issue Sep 19, 2017

Generate compile time error for table! double import
Generates a better error message when a column
shares the same name as its table.

Fixes diesel-rs#624

notryanb added a commit to notryanb/diesel that referenced this issue Sep 20, 2017

Generate compile time error for table! double import
Generates a better error message when a column
shares the same name as its table.

Fixes diesel-rs#624

@sgrif sgrif closed this in #1175 Sep 20, 2017

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