Search code examples
rustrust-sqlx

missing field `registration_key` in initializer of `database::models::RegistrationKeys`


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.


Solution

  • 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