loco-rs / loco

🚂 🦀 The one-person framework for Rust for side-projects and startups
https://loco.rs
Apache License 2.0
5.45k stars 235 forks source link

Compilation fails after cargo db entities of existing postgresql schema #971

Open biruburu opened 2 weeks ago

biruburu commented 2 weeks ago

Description

I have an existing postgresql schema that I wish to "uplift" into loco. Using regular sea-orm-cli (without loco), I am able to successfully generate entities using the following command:

sea-orm-cli generate entity \
    --output-dir src/models/_entities \
    --database-url "${database_url}" \
    --with-serde both \
    --serde-skip-deserializing-primary-key \
    --expanded-format

I can then compile and use the generated entities as expected.

To Reproduce

When I try to use cargo loco db entities instead of sea-orm-cli, the entities get generated. However, compilation fails with:

error[E0277]: the trait bound `organization::ActiveModel: sea_orm::ActiveModelBehavior` is not satisfied
  --> src/models/_entities/organization.rs:6:35
   |
6  | #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
   |                                   ^^^^^^^^^^^^^^^^^ the trait `sea_orm::ActiveModelBehavior` is not implemented for `organization::ActiveModel`
   |
   = help: the following other types implement trait `sea_orm::ActiveModelBehavior`:
             _entities::client::ActiveModel
           and 46 others
note: required by a bound in `sea_orm::EntityTrait::ActiveModel`
  --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sea-orm-1.1.1/src/entity/base_entity.rs:62:23
   |
62 |     type ActiveModel: ActiveModelBehavior<Entity = Self>;
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EntityTrait::ActiveModel`
   = note: this error originates in the derive macro `sea_orm::prelude::DeriveEntity` (in Nightly builds, run with -Z macro-backtrace for more info)

To Reproduce

  1. cargo install loco-cli
  2. loco new --path loco --name test --db postgres --template saas --assets serverside
  3. cd loco/test
  4. Set config/development.yaml to point to a moderately complex postgres database that was not generated via loco
  5. cargo loco db entities
  6. cargo build

Expected Behavior

The entities get generated and cargo build succeeds.

Environment:

This is a fresh installation of loco==0.12.0 on MacOS 15.0.1 using rustc --version 1.81.0. I am using postgresql 15.x, and tried sea-orm-cli 1.0.1, 1.1.0, 1.1.1 (all work fine when used directly.)

Additional Context

Edit: the issue seems to be that cargo loco db entities skips certain models when generating models/mod.rs (see first comment below). There does not seem to be anything different between these models and the ones that are actually generated:

// skipped in mod.rs:
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "Text", unique)]
    pub uuid: String,
    #[sea_orm(column_type = "Text", unique)]
    pub name: String,
    #[sea_orm(column_name = "createdAt")]
    pub created_at: Option<DateTimeWithTimeZone>,
    #[sea_orm(column_name = "updatedAt")]
    pub updated_at: Option<DateTimeWithTimeZone>,
}

// generated in mod.rs:
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(unique)]
    pub uuid: Uuid,
    #[sea_orm(column_type = "Text", unique)]
    pub email: String,
    #[sea_orm(column_name = "createdAt")]
    pub created_at: Option<DateTimeWithTimeZone>,
    #[sea_orm(column_name = "updatedAt")]
    pub updated_at: Option<DateTimeWithTimeZone>,
}

Original: I tried passing cargo loco db entities --with-serde both --serde-skip-deserializing-primary-key --expanded-format, but received an argument --with-serde not recognized error.

Those arguments seem to be required for my sea-orm-cli entities to work. Is there a way to pass them or configure cargo loco db entities to use them?

biruburu commented 2 weeks ago

It looks like cargo loco db entities failed to add a number of generated entities to src/models/mod.rs. Adding them to mod.rs manually makes it compile.

I guess the bug here lies in why they are not being added to mod.rs.

Let me know how I can help debug this!