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

Entity generation for partitioned tables #1582

Closed
jared-mackey opened this issue Apr 6, 2023 · 5 comments · Fixed by SeaQL/sea-schema#105
Closed

Entity generation for partitioned tables #1582

jared-mackey opened this issue Apr 6, 2023 · 5 comments · Fixed by SeaQL/sea-schema#105
Assignees

Comments

@jared-mackey
Copy link
Contributor

Description

Hi,

I'm not certain this is a bug but I don't know if it's correct behavior either. When generating entities for my schema I noticed that the entity generator does a few unexpected things with my partitioned tables:

  • It tries to generate an entity for each partition instead of just one for the super table.
    • I was able to ignore these though via the CLI parameters.
  • Entity relations are being created for table that FK to the super table but one per partition.

Steps to Reproduce

  1. Create a partitioned table. Let's call this table_1 and then each partition will be table_1_p1 through table_1_p128.
  2. Create a normal table that FKs to the partition table. Let's call this table_2.
  3. Generate the entities with sea-orm-cli generate entity -o entity/src --lib --tables "table_1,table_2" --ignore-tables "table_1_p1,table_1_p2..." --max-connections 25

Expected Behavior

  • I would not get entities created for table_1_p1..table_1_p128.
  • Entity for table_2 would have a single relationship defined -- the one to table_1.

Actual Behavior

✅ I did not get entities created for table_1_p1
✅ The table_2 entity correctly had a FK to the main table.
❌ The table_2 entity was filled with relationships to table_1_p1..table_1_p128.

Example

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::table_1_p34::Entity", // <-- actually doesn't exist because I don't generate those entities
        from = "Column::Table1Id",
        to = "super::table_1_p34::Column::Id", // <-- same
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Table1P34,
...
}

impl Related<super::table_2_p34::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Table1P34.def()
    }
}

Reproduces How Often

Every time.

Workarounds

Delete the wrongly generated code.

Versions

0.11.2

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 7, 2023

Can you create a new folder under /issues, attach a minimal schema and open a PR?
Schema discovery do not take in consideration partitioned tables, so we'll need to figure out how to identify and remove the duplicates.

@cemremengu
Copy link
Contributor

cemremengu commented Apr 7, 2023

As a note, other libraries like jooq (java) provide a regex mechanism to exclude unwanted tables like partitions in a schema such as

<excludes>.*_p[0-9_]+</excludes>

@billy1624
Copy link
Member

Hey @jared-mackey, thanks for the report! I just drafted a PR to skip parsing partitioned Postgres tables.

@billy1624
Copy link
Member

The patch will be released in the coming 0.12.x

billy1624 pushed a commit that referenced this issue May 3, 2023
@billy1624 billy1624 reopened this May 3, 2023
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

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

Successfully merging a pull request may close this issue.

4 participants