floor icon indicating copy to clipboard operation
floor copied to clipboard

Left join doesn't return List<Object> instead returns null

Open utkarshmarwaha opened this issue 3 years ago • 1 comments

I have two entities,

@Entity(
  tableName: "Articles",)
class Article {
  @primaryKey
  int? id;
  String? categoryIds;
  String? mediaType;
  String? title;
  String? image;
  String? video;
  String? publishedOn;
  String? url;}

and

@Entity(
tableName: "ArticleBookmark", foreignKeys: [
  ForeignKey(
      entity: Article,
      parentColumns: ["id"],
      childColumns: ["articleId"],
      onDelete: ForeignKeyAction.cascade,
      onUpdate: ForeignKeyAction.noAction)
], indices: [
  Index(value: ["articleId"])
])
class ArticleBookmark {
  @primaryKey
  int? articleId;
  bool isBookmark = false;

  ArticleBookmark({
    this.articleId,
    this.isBookmark = false,
  });
}

I Want to fetch the values of both the tables in another object(ArticleDetail) which isn't an entity.

ArticleDetails as follows:

class ArticleDetail {
  late Article article;
  late ArticleBookmark articleBookmark;

  ArticleDetail.name({required this.article, required this.articleBookmark});

}

I am using this following Query:

@Query('SELECT Articles.*, ArticleBookmark.* from Articles LEFT JOIN ArticleBookmark on Articles.id = ArticleBookmark.articleId ORDER BY dateCreated DESC') Future<List<ArticleDetail>?> getAllArticleItems();

The List<ArticleDetail> returned by getAllArticleItems() method comes out to be null.

Please suggest, how can I use Left join to fetch the value from two entities and consolidate it together in a single object.

utkarshmarwaha avatar Aug 17 '21 19:08 utkarshmarwaha

Would this be something that view can resolve? https://pinchbv.github.io/floor/database-views/

SEGVeenstra avatar Jul 04 '23 15:07 SEGVeenstra