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

Support relations with @Relation annotation #47

Open
vitusortner opened this issue Feb 13, 2019 · 20 comments
Open

Support relations with @Relation annotation #47

vitusortner opened this issue Feb 13, 2019 · 20 comments
Labels
feature Request and implementation of a feature (release drafter)

Comments

@vitusortner
Copy link
Collaborator

No description provided.

@vitusortner vitusortner modified the milestones: 2, Version 1.0 Feb 13, 2019
@vitusortner vitusortner added feature Request and implementation of a feature (release drafter) and removed enhancement labels Mar 5, 2019
@pishguy
Copy link

pishguy commented Jul 23, 2019

the library can be support relation ship?

@vitusortner vitusortner removed this from the 2 milestone Jul 23, 2019
@vitusortner
Copy link
Collaborator Author

This is planned for a future release of the library. Unluckily, I can't tell when it's going to be.

@hsul4n
Copy link

hsul4n commented Mar 1, 2020

Any updates @vitusortner

@locskot
Copy link

locskot commented Mar 22, 2020

Hi there!
Is there any support to one-to-many relation? How can i get stored in db complex object (not just simple strings and ints but also list)?

@ForceTower
Copy link

If you guys ever used Room (the library this is based), and checked out the generated code, you may write the boilerplate yourself and get the relations.

In my case I have the entities Article, Author and ArticleAuthor (that assigns a article to an author).

I started playing with flutter/dart this week, so you might see some improvements in the code:
https://gist.github.com/ForceTower/1d9acd12e772fe352bedcd5e640de7e2

In this case, AuthorArticleId is a DatabaseView, since this library at this time doesn't support @Embedded or map the database query to any model class.

The query code of the DatabaseView can be anything, it just exists so the code generator knows how to map the query result to the model I want.

The line https://gist.github.com/ForceTower/1d9acd12e772fe352bedcd5e640de7e2#file-article_dao-dart-L15 is horrible, Room fetches the results and places them into a HashMap based on the pivot table id (in my case, articleId) to increase performance, since findWhere is a O(N) and there might be a lot of models to go through.

@proninyaroslav
Copy link

proninyaroslav commented Oct 28, 2020

@ForceTower
This is not a complete implementation. Here you only listen Article table (one stream), and after changing Article, you grab the data from AuthorArticleId. But what if the data in AuthorArticleId is mutable, and you need to listen changes from Article and AuthorArticleId at the same time (equivalent to listening two streams)?

@ForceTower
Copy link

@proninyaroslav The implementation I shared does indeed has a flaw, if the author itself changes, the changes of it are not propagated into the stream.

I believe the desired behavior would be achievable by listening to a second stream (of authors) and publishing snapshots of both in the result stream. But that seems like a code smell :(

Or maybe to create and dependency on the first query to both tables, this way one single stream would do the job correctly.

@proninyaroslav
Copy link

@ForceTower
Android Room uses Observable to listen selected tables. I think we can use FloorDatabase.changeListener for this purpose.

@BrianMwas
Copy link

If you guys ever used Room (the library this is based), and checked out the generated code, you may write the boilerplate yourself and get the relations.

In my case I have the entities Article, Author and ArticleAuthor (that assigns a article to an author).

I started playing with flutter/dart this week, so you might see some improvements in the code:
https://gist.github.com/ForceTower/1d9acd12e772fe352bedcd5e640de7e2

In this case, AuthorArticleId is a DatabaseView, since this library at this time doesn't support @Embedded or map the database query to any model class.

The query code of the DatabaseView can be anything, it just exists so the code generator knows how to map the query result to the model I want.

The line https://gist.github.com/ForceTower/1d9acd12e772fe352bedcd5e640de7e2#file-article_dao-dart-L15 is horrible, Room fetches the results and places them into a HashMap based on the pivot table id (in my case, articleId) to increase performance, since findWhere is a O(N) and there might be a lot of models to go through.

Hi @ForceTower I saw the SQL statement used supports one to one relationship how would I manage a one to many kind of relationship. In this case, you have an author with many articles. I would as a result get a List of authors with lists of articles for each of them.
Any help would be amazing

@ForceTower
Copy link

Hi @ForceTower I saw the SQL statement used supports one to one relationship how would I manage a one to many kind of relationship. In this case, you have an author with many articles. I would as a result get a List of authors with lists of articles for each of them.
Any help would be amazing

Hey there, in the example I shared, I believe the tables were set up to a N..N relationship, So... lets assume you have a proper 1..N relation, like a movie and it's posters :D

class MoviePoster {
  final int id;
  final String image;
  final int movieId;
  MoviePoster(this.id, this.image, this.movieId);
}

If I remember correctly to implement a 1..N room does something like:
1 - Fetch the movies
2 - Map the movie list where for each movie, you select from poster table based on the movie id (this will give you a list of posters) and create an object with the movie and the posters list.
3 - Return the mapped list

The secret behind the N..N relationship is the line https://gist.github.com/ForceTower/1d9acd12e772fe352bedcd5e640de7e2#file-article_dao-dart-L15 there I did a firstWhere which returns a single author, if you just do a where , you'd have a List<Author> for an Article.
To do what you asked basically invert the 2 tables used in the example.

But... Be aware of the error @proninyaroslav pointed out in this example

@BrianMwas
Copy link

BrianMwas commented Jul 9, 2021 via email

@mt-akar
Copy link

mt-akar commented Aug 7, 2021

Any updates on the @relation? It makes data retrieval so much easier.

This is very important to me. I will settle with the first decent ORM I find that supports this feature.

@DesmondFox
Copy link

Any updates?

1 similar comment
@JoeSteven
Copy link

Any updates?

@Velkamhell97
Copy link

Any updates ?

@taosif7-dreamorbit
Copy link

Any Updates?

@Artem-S1
Copy link

Any updates?

@juanes30
Copy link

Any updates?

@ngminhkhoa
Copy link

Any updates ?

1 similar comment
@adlerbn
Copy link

adlerbn commented Apr 23, 2024

Any updates ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Request and implementation of a feature (release drafter)
Development

No branches or pull requests