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

Issue in using infer_schema! #309

Closed
inxi-pc opened this Issue Apr 27, 2016 · 7 comments

Comments

Projects
None yet
3 participants
@inxi-pc

inxi-pc commented Apr 27, 2016

Hi,

When i flow the guide book to learn diesel using, i create schema.rs file and add the

content :

infer_schema!(env::var("DATABASE_URL").expect("DATABASE_URL must be set"));

Because my project .env file has been used, and it is toml format, not be compatible.

So i create a new file named env.diesel, and in my main.rs file, add this content:

    dotenv::from_filename("env.diesel").ok();

But i got the comlier error:

src/database/schema.rs:3:15: 3:74 error: expected string literal
src/database/schema.rs:3 infer_schema!(env::var("DATABASE_URL").expect("DATABASE_URL must be set"));
                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/database/schema.rs:3:1: 3:76 note: in this expansion of infer_schema! (defined in src/main.rs)
error: aborting due to previous error

Could you help me? thanks very much.

@mfpiccolo

This comment has been minimized.

Collaborator

mfpiccolo commented Apr 27, 2016

The recommended approach is to use the dotenv! macro provided by the doenv crate.

infer_schema!(dotenv!("DATABASE_URL"))

@inxi-pc

This comment has been minimized.

inxi-pc commented Apr 27, 2016

@mfpiccolo hi, thanks..but could i use other way like my mentioned, because my .env file is using in project, and format is not compatible dotenv..

@mfpiccolo

This comment has been minimized.

Collaborator

mfpiccolo commented Apr 27, 2016

Both infer_schema! and dotenv! are compiler plugins which are expanding out code. This means that dotenv! will expand out to be a string literal at compile time. You can use env! in the standard library but you will have to get the the env set during compile time.

https://doc.rust-lang.org/std/macro.env!.html

@inxi-pc

This comment has been minimized.

inxi-pc commented Apr 27, 2016

@mfpiccolo you means my mentioned way is in run time, so we cant use this. Because the macro! only can accept the string literal?

infer_schema!(env::var("DATABASE_URL").expect("DATABASE_URL must be set"));

The way in complier time is a little not friendly..

i have a matter, dotenv! also get content from file, if we can set a string literal param for dotenv!?

@mfpiccolo

This comment has been minimized.

Collaborator

mfpiccolo commented Apr 27, 2016

I mean you could just pass the database url to infer_schema!

infer_schema!("postgresql://localhost/some_database");

@mfpiccolo

This comment has been minimized.

Collaborator

mfpiccolo commented Apr 27, 2016

Seems like what you really are looking for is a way to load ENV from another file through the dotenv! plugin. You may want to open an issue here if that is the case https://github.com/slapresta/rust-dotenv.

@sgrif

This comment has been minimized.

Member

sgrif commented Apr 27, 2016

You can pass a string literal, or compile time macro that generates a string literal (like dotenv! if you're using the rust-dotenv crate, or env! from the standard library).

The way in complier time is a little not friendly..

That's the tradeoff of a compiled language. We can't just evaluate arbitrary Rust code.

@sgrif sgrif closed this Apr 27, 2016

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