also need to specify schema in connection as i use either web or api schema, not default public.
My db.rs
use sqlx::{PgPool, Pool, Postgres};
use std::env;
pub async fn init_pool() -> Pool<Postgres> {
let database_url =
env::var("DATABASE_URL").expect("DATABASE_URL must be set in the environment");
PgPool::connect(&database_url)
.await
.expect("Failed to create database pool")
}
and in main.rs
let pool = db::init_pool().await;
let app = Router::new()
...routes here...
.with_state(pool);
PGHOST=localhost
PGPORT=5432
PGUSER=postgres
PGPASSWORD=admin
PGDATABASE=postgres
use sqlx::{postgres::PgConnectOptions, PgPool, Pool, Postgres};
pub async fn init_pool() -> Pool<Postgres> {
let options = PgConnectOptions::new();
PgPool::connect_with(options)
.await
.expect("Failed to connect to db 1.")
}
and it worked until i changed one of the sqls now all have the same error
That's a problem with the query! macro. It needsDATABASE_URL to be set during build time (it can read it from .env) to be able to statically check your queries against your database tables. If you don't need support for statically checked queries, you can just use the sqlx::query function instead of the macro.
That feature is a big reason i started to learn rust. Oh well db_url it is then.
Do you know how to add schema into that url? Or is it not supported too and i need to include schema in sql myself?