launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.39k stars 1.27k forks source link

Cannot get fields when mapping a query #2406

Open JaydenElliott opened 1 year ago

JaydenElliott commented 1 year ago

Bug Description

When attempting to use query! with map , I cannot get any of the rows out of the record and receive the error field "mood" of "Record" is private

The strange thing this is that when doing this in a test environment with #[sqlx::test] the error disappears.

Minimal Reproduction

With the following table

CREATE TABLE IF NOT EXISTS test (mood SMALLINT NOT NULL, template TEXT NOT NULL);

and query

   let moods: Vec<i16> = sqlx::query!("select mood from test")
            .map(|f| f.mood)
            .fetch_all(&self.pool)
            .await?;

Info

itszero commented 1 year ago

I also run into the same problem. However, the error only shows up on rust-analyzer (2023-03-13). My rustc (rustc 1.67.0-nightly (edf018221 2022-11-02)) would happily compiles the code.

galah92 commented 1 year ago

Getting the same error when running the sqlite/todos example with SQLx version 0.6.2. It also seems to be relevant only for rust-analyzer, since cargo check and clippy do not complain.

sameul2012 commented 1 year ago

Same error.

Minimal Reproduction

CREATE TABLE
  public.articles (
    id serial NOT NULL,
    created_at timestamp without time zone NOT NULL DEFAULT now(),
    title character varying(255) NOT NULL,
    content text NOT NULL,
    date date NOT NULL DEFAULT CURRENT_DATE
  );

ALTER TABLE
  public.articles
ADD
  CONSTRAINT articles_pkey PRIMARY KEY (id)
create sequence 
blog_id_seq
increment 1
minvalue 1
maxvalue 9223372036854775807
start with 1
cache 1
insert into "public"."articles" ("content", "created_at", "date", "id", "title") values ('Hello everyone', '2023-03-16 17:42:45.071063', '2023-03-16', 2, 'Welcom');
/// get one article per id
pub async fn get_article(
    id: Path<(u32,)>,
    state: State<Arc<AppState>>,
) -> Result<Json<Article>, CustomError> {
    let db_pool = &state.db_pool;

    let article = sqlx::query!(
        "SELECT title, content, date FROM articles WHERE id = $1", 
        id.0 as i32
    )
    .fetch_one(db_pool)
    .await?;

    let article = Article{
        id: None,
        title: article.title.clone(),
        content: article.content.clone(),
        date: Some(article.date),
    };

    Ok(Json(article))
}

Info

SQLx version: 0.6.2 SQLx features enabled: ["runtime-tokio-rustls", "postgres", "macros" , "chrono"] Database server and version: Postgres 15.2 Operating system: macOS 12.6.1 rustc --version: 1.70.0 nightly