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

Insertable derive may error if used with other schema features #1032

Closed
skade opened this Issue Jul 21, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@skade

skade commented Jul 21, 2017

Setup

Versions

  • Rust: 1.20 nighly
  • Diesel: 0.14.0
  • Database: Postgres
  • Operating System Linux/mac

Feature Flags

  • diesel: postgres
  • diesel_codegen: postgres

Problem Description

When importing schema dsl methods in the same namespace as an Insertable derive, the code doesn't compile.

#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_codegen;

mod schema {
    infer_schema!("dotenv:DATABASE_URL");
}

mod models {
    use super::schema::posts::dsl::posts;

    #[derive(Queryable)]
    pub struct Post {
        pub id: i32,
        pub title: String,
        pub body: String,
        pub published: bool,
    }

    #[derive(Insertable)]
    #[table_name="posts"]
    pub struct NewPost<'a> {
        pub title: &'a str,
        pub body: &'a str,
    }
}

https://gist.github.com/skade/9f66885d041de7f8cf88ce3788696ae1

What are you trying to accomplish?

Nothing, it just occured to me.

What is the expected output?

cargo build
   Compiling diesel-derive-error v0.1.0 (file:///Users/skade/src/diesel-derive-error)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38 secs

What is the actual output?

See linked gist above

Are you seeing any additional errors?

No.

Steps to reproduce

Use the file linked in the gist as lib.rs with your standard example setup.

Checklist

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

This comment has been minimized.

Member

sgrif commented Jul 21, 2017

This is the expected behavior. The module needs to be in scope. You generally shouldn't import stuff from dsl outside of functions

@sgrif sgrif closed this Jul 21, 2017

@killercup

This comment has been minimized.

Member

killercup commented Jul 21, 2017

@skade

This comment has been minimized.

skade commented Jul 21, 2017

The example is minimal, this also happens if the schema is in scope.

This is documented nowhere and came up in an innocuous attempt to extend this (working) module by following your guides, by inserting NewCampaign.

https://github.com/rust-community/rust-campaigns-server/blob/master/src/main.rs#L47-L112

It is at least surprising and the error message is terribly unhelpful.

I find it confusing that diesel needs very tight hygienic control of the surrounding namespace or otherwise falls flat on its nose.

@skade skade referenced this issue Jul 21, 2017

Merged

Refactorings #1

@sgrif

This comment has been minimized.

Member

sgrif commented Jul 24, 2017

I agree that we need to make this more clear in the documentation. I'm not sure how we can improve the error message other than a lint.

I find it confusing that diesel needs very tight hygienic control of the surrounding namespace or otherwise falls flat on its nose.

I don't see any way that we could work around this, unless we enforce the exact location of the table module from the crate root, which isn't something I want to do.

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