NyxCode / ormx

bringing orm-like features to sqlx
MIT License
287 stars 32 forks source link

Using attribute `default` with `custom_struct` produces strange compile errors #20

Closed LouisGariepy closed 1 month ago

LouisGariepy commented 2 years ago

Sorry for not having a more minimal example (I might post one soonish) but this should be fairly straightforward if not minimal.

This code gives : wildcard overrides are only allowed with an explicit record type, e.g. query_as!() and its variants

#[derive(Debug, ormx::Table)]
#[ormx(
    table = "Account",
    id = id,
    insertable,
    deletable
)]
pub struct Account {
    #[ormx(default, get_many=get_many_by_id)]
    id: i32,
    #[ormx(get_one, get_many=get_many_by_email, set)]
    email: String,
    #[ormx(set)]
    password_hash: String,
    #[ormx(default)]
    creation_time: OffsetDateTime,
    #[ormx(default, custom_type, set)]  //<-- removing the default attribute fixes the compile error
    account_type: AccountType,
    #[ormx(default, set)]
    suspended: bool,
}

Here's the custom type in question :

#[derive(
    PartialEq, Debug, Copy, Clone, sqlx::Type, Serialize, Deserialize, AsRefStr, IntoStaticStr,
)]
#[sqlx(type_name = "AccountType")]
pub enum AccountType {
    Temporary,
    Guest,
    Admin,
}

And here's the database schema I'm compiling against :

CREATE TABLE Account
(
    id            SERIAL UNIQUE,
    email         TEXT PRIMARY KEY,
    password_hash TEXT                     NOT NULL,
    creation_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    account_type  AccountType              NOT NULL DEFAULT 'Temporary',
    suspended     BOOLEAN                  NOT NULL DEFAULT false
);
LouisGariepy commented 2 years ago

I will close the issue for now since I no longer have interest in isolating the underlying problems. It also seems I'm the only one hitting this particular problem.

NyxCode commented 2 years ago

Let me reopen this until I get a chance to take a look myself.

benluelo commented 2 years ago

I am getting this issue as well, using a newtype id field. Has there been any progress on this? I would be happy to help out in fixing it.

benluelo commented 2 years ago

Update on this: it seems that replacing the [...] RETURNING field AS \"field: _\" with [...] RETURNING field AS \"field: ConcreteType\" fixes the issue. I will put together a PR!

NyxCode commented 1 month ago

fixed in #40