'Error in an unknown field, when it actually exists

I have the following code, using the anchor framework (version 0.23.0):

use anchor_lang::prelude::*;

#[account]
#[derive(Default, Debug)]
pub struct Raffle {
    // ... fields 
}

#[account(zero_copy)]
pub struct Participant {
    // ... fields
}

#[derive(Accounts)]
pub struct BuyTicket<'info>{
    #[account(has_one = participants)]
    pub raffle: Account<'info, Raffle>,
    #[account(mut)]
    pub participants: AccountLoader<'info, Participant>,
    // ... others fields
} 

Then, when I run anchor build, I get the following error:

BPF SDK: ~/.local/share/solana/install/releases/1.9.13/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
   Compiling project v0.1.0 (~/programs/project)
error[E0609]: no field `participants` on type `anchor_lang::prelude::Account<'_, structs::Raffle>`
   --> programs/project/src/structs.rs:136:25
    |
136 |     #[account(has_one = participants)]
    |                         ^^^^^^^^^^^^ unknown field

For more information about this error, try `rustc --explain E0609`.
error: could not compile `nft2raffle` due to previous error

Now, rustc --explain E0609 defines the error of accessing a nonexistent field within a struct; but the participant field inside the structure does exist.

How can fix the error?



Solution 1:[1]

has_one checks the target_account field participant on the account raffle matches the key of the target_account participant field in the Accounts struct.

You have to put participants: Pubkey in struct Raffle

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 batphonghan