Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
biruburu opened this issue Nov 7, 2024 · 2 comments
Open
Milestone

Comments

@biruburu
Copy link

biruburu commented Nov 7, 2024

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
Copy link
Author

biruburu commented Nov 7, 2024

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!

@kaplanelad kaplanelad added this to the 0.13.1 milestone Nov 20, 2024
@jondot
Copy link
Contributor

jondot commented Nov 24, 2024

Hi, I believe this is because we're augmenting the generated entities right after using seaorm-cli to generate. But something in your schema is turning up as unexpected.
See here: https://github.com/loco-rs/loco/blob/master/src/db.rs#L340

I'm wondering if you can share that schema so we can repro on it?

@jondot jondot modified the milestones: 0.13.1, 0.14.0, 0.15.0 Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants