0

Here is my struct definition

pub struct RegistrationKeys {
    pub id: i64,
    pub registration_key: String,
}

Here's my table definition

CREATE TABLE
    IF NOT EXISTS RegistrationKeys (
        id SERIAL PRIMARY KEY,
        registration_key TEXT,
        owned BOOLEAN DEFAULT FALSE,
        owned_by INTEGER
    );

I'm attempting to grab the id field by the registration_key field

let registration_key = String::from("ABCD"); // I've also tried using &str
let response = query_as!(
    RegistrationKeys,
    "SELECT id FROM RegistrationKeys WHERE registration_key = $1",
    registration_key
)
.fetch_one(&pool)
.await;

I feel like it's expecting me to somehow bind the query result to registration_key like String::from("RESULT") but I can't figure out how I would do this nor find any examples anywhere.

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
Mickers
  • 1,367
  • 1
  • 20
  • 28

1 Answers1

1

You're trying to partially intialize your RegistrationKeys struct, you should consider retrieving the registration_key as well from your query:

SELECT id, registration_key FROM RegistrationKeys WHERE registration_key = $1
DrLarck
  • 360
  • 1
  • 12
  • That helped a lot! I also had to restructure my query as well as wrap my field names with quotes like below `query_as::<_, RegistrationKeys>(r#"SELECT "id", "registration_key" FROM RegistrationKeys WHERE "registration_key" = $1;"#,).bind(registration_key)` – Mickers Aug 07 '23 at 16:10