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

Foreign Key Marker #388

Closed
liabri opened this issue Dec 17, 2021 · 9 comments
Closed

Foreign Key Marker #388

liabri opened this issue Dec 17, 2021 · 9 comments

Comments

@liabri
Copy link

liabri commented Dec 17, 2021

Hello,

I am wondering of the possibility to mark foreign_keys in models the same way primary_keys are marked. This would allow the entry of the foreign_key to exist directly inside the model. I imagine the syntax to be something like: #[sea_orm(foreign_key)], in the case of Fruit and Cake (as per the example provided in examples/basic) I would imagine it to be like this:

pub struct Fruit {
    #[sea_orm(primary_key)]
    id: u32,
    name: String,
    #[sea_orm(foreign_key)] //referring to the primary_key of the model "Cake" as marked in the data type in the line below
    cake: Cake, //note here, instead of cake_id it would contain the Cake directly
}

pub struct Cake {
    #[sea_orm(primary_key)]
    id: u32,
    name: String,
}

Queries utilising JOIN should make this quite feasible, but if the idea of containing the model directly within another model destroys the model (I cannot think of a synonym x)) of this project, maybe this foreign_key marker could reduce boiler plate for defining relations.

@billy1624
Copy link
Member

Hey @lbcat06, welcome!! I see the potential use case of it in web API.

I'm thinking if it's only for selecting data from the database (readonly)? Or, it's mutable and can use for saving data back into the database?

@liabri
Copy link
Author

liabri commented Dec 18, 2021

I am not too well versed in SQL so I'm not quite sure whether writing operations using JOIN are feasible (I do think it is, so I think making it mutable would be great), but I am sure it would be feasible for readonly JOIN operations. Ideally it would be both, as this would allow to denormalise the data for more intuitive use within APIs and such.

@tyt2y3
Copy link
Member

tyt2y3 commented Dec 18, 2021

I think the type definition of cake: Cake is bad as it makes the size of the struct much larger.
It could better be cake: Option<Box<Cake>>, which means 'a nullable pointer to Cake'.
Then it leaves open a lot of API behaviour to define.

@liabri
Copy link
Author

liabri commented Dec 20, 2021

I think the type definition of cake: Cake is bad as it makes the size of the struct much larger. It could better be cake: Option<Box<Cake>>, which means 'a nullable pointer to Cake'. Then it leaves open a lot of API behaviour to define.

Sounds like a good idea, I am not sure how passing that over a web api would work though.

@PauMAVA
Copy link

PauMAVA commented Jan 2, 2022

Hey! I am pretty interested in this feature being added to sea-orm. I would even be willing to develop it myself.

First of all regarding @lbcat06 concern on how a Box<T> would show over web API I have tested it using a really simple rocket app you can find in this gist. There is no problem in sending a Box<T> via API as serde automatically implements Serialize for Box<T> whenever T implements Serialize.
imagen

Regarding the feature, I have some ideas on how to implement this but I am not sure 100% on where to start and the implications of an addition like this to sea-orm. I think there are two main parts for the development of this feature:

  • Modifying the executor part to correctly obtain and build the associated struct.
  • Creating the appropriate codegen and macros for code generation.

Apart from this, I have multiple questions:

  • It would be better to use a JOIN clause and build the struct using an identifier for the associated table so that repeated fields (with the same name) on both tables can be identified correctly? Or maybe a subquery?
  • Basically I would also need a little bit of help and advice from someone that knows sea-orm better as I am pretty new to this project.

Cheers! 😄

@billy1624
Copy link
Member

billy1624 commented Jan 2, 2022

Hey @PauMAVA, thanks for reaching out!! We love to see more and more contributors joining in!

@RomainMazB are you working on this? #415

@RomainMazB
Copy link

@RomainMazB are you working on this? #415

I am working on #415 yes (not the foreign_key). I have to admit that I may have to learn some few things about trait types and other stuff in the rust's typing-system to succeed but yes, I'm actively working on it.

@PauMAVA
Copy link

PauMAVA commented Jan 3, 2022

Hello @RomainMazB ! That's good as we will need the save methods in order to make the foreign_key feature writeable. We can continue discussing the API you propose in #415.

@tyt2y3
Copy link
Member

tyt2y3 commented Feb 21, 2023

As explained in https://www.sea-ql.org/blog/2022-05-14-faq-01/ this API style is not the best fit for Rust

@tyt2y3 tyt2y3 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

5 participants