-
-
Notifications
You must be signed in to change notification settings - Fork 518
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
[sea-orm-cli] Better handling of relation generations #239
Comments
Hey, this doesn't look like too hard of an issue, can I take it? Also, is this repo participating in Hacktoberfest (or rather, would you be willing to add the |
Well, I think the 'skipping' part should be reasonably easy if you are already familiar with Rust's code generation facilities. The 'Link' generation part could be substantial, but I imagine it being very similar to the original 'Related' implementation. We can separate these two tasks as two PRs. Sure. Oh, I was not aware how to join Hacktoberfest. Indeed. Seems fun!
https://hacktoberfest.digitalocean.com/faq I added the topic to our repo now. It's kind of sad because we are already half way through October ( Let us know if there is anything we can help to help you earn a reward! |
@AngelOnFira do you want to assign this to yourself or you have other ideas? |
Ya, if I can be assigned this it would be good 😄 I just got caught up in the other PR I was working on 😛 |
So I'm working on what's the minimum work that has to be done to remove the duplication. I've set up two simple tables for testing, where one table has two columns that relate to the other: (The naming might be a little confusing, imagine it's So getting this with the #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::FromAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FormsAccount,
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::ToAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FormsAccount,
} Now, if our solution was simply to de-duplicate these so that there is only one relation, this would require throwing away data about one of the relations. For example: #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::FromAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FormsAccount,
} On the other hand, this solution wouldn't be too bad for the accounts relation. By default, it looks like: #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::forms_transaction::Entity")]
FormsTransaction,
#[sea_orm(has_many = "super::forms_transaction::Entity")]
FormsTransaction,
} This would be quite easy to resolve. So going back to the transaction relations, I just made a simple tweak to include the column name as part of the #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::FromAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FromAccountIdFormsAccount,
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::ToAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
ToAccountIdFormsAccount,
}
impl Related<super::forms_account::Entity> for Entity {
fn to() -> RelationDef {
Relation::FromAccountIdFormsAccount.def()
}
}
impl Related<super::forms_account::Entity> for Entity {
fn to() -> RelationDef {
Relation::ToAccountIdFormsAccount.def()
}
} Although this allows for both to be unique as a I wanted to write this down to get some feedback, but also because it helped me try to scope out the problem a little better. |
Yes. I think removing relation generation would be the first step. It compiles and at least remove the confusion to users. sea-orm/src/tests_cfg/entity_linked.rs Lines 1 to 34 in e2c8d32
|
@tyt2y3 But I am still wondering, what should happen in a case where multiple similar relations have different attribute macros? Should I just discard any that aren't the first? #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::FromAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FormsAccount,
#[sea_orm(
belongs_to = "super::forms_account::Entity",
from = "Column::ToAccountId",
to = "super::forms_account::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
FormsAccount,
} |
I think naming the enum variants differently would be good. May be |
Ya, this should work for a temporary solution, but in the long term I definitely think it should be based on the column name, or another alternative. |
Hey @AngelOnFira, any updates on this? :P |
Thanks for checking in, I need this for a project I have coming up, but I've been super busy with other work. As soon as I do have time, I do plan on hopping on this issue since it's quite important to me, but if someone else has time to work on it right now, I'd be very grateful for that as well 😄 |
Thanks!! @AngelOnFira. Take your time! |
Hey @AngelOnFira, I have just open a PR for this. You can check it if you have time :)) |
* With clause and with queries * Testing INSERT with CTE * Allow arbitrary subqueries
It has been reported that the codegen can generate non-compilable code when dealing with relations.
As the first step, we can simply skip generating the relations if:
Later, we can also try to generate Links inplace of those relations.
The text was updated successfully, but these errors were encountered: