【问题标题】:Why many to one with backlink not allowed in objectbox-dart?为什么在 objectbox-dart 中不允许使用反向链接的多对一?
【发布时间】:2021-04-29 07:36:53
【问题描述】:

我有这个结构:

@JsonSerializable()
class Media {
  @Id(assignable: true)
  int id = 0;
  int lid = 0;
  String url = '';
  String? title;
}

@Entity()
class NewsPicture extends Media {
  @override
  @Id(assignable: true)
  int id = 0;

  @Backlink('newsPicture')
  final news = ToOne<News>();
}

@JsonSerializable(explicitToJson: true)
@Entity()
class News extends Data<News> implements DataInterface<News> {
  @Id(assignable: true)
  @JsonKey(defaultValue: 0)
  int lid = 0;
  final Picture = ToMany<NewsPicture>();
}

在生成过程中,objectbox_generator:resolver 给了我这个错误信息:

@Backlink() 注释的无效使用 - 只能用于 ToMany 字段

为什么不允许?我错过了什么?

【问题讨论】:

    标签: flutter dart objectbox


    【解决方案1】:

    它在那个方向上不受支持,因为它并不是真正需要的(对任何事情都没有帮助),您可以翻转方向/更改存储关系的位置。此外,ToOne 关系的存储效率更高,因为它们只是数据库中的一个字段,而独立的 ToMany 关系需要一个中间“映射表”。

    如果你像这样更新你的模型,它就会工作,你不会看到在你的应用中使用它的方式有什么不同:

    @Entity()
    class NewsPicture extends Media {
      @override
      @Id(assignable: true)
      int id = 0;
    
      final news = ToOne<News>();
    }
    
    @JsonSerializable(explicitToJson: true)
    @Entity()
    class News extends Data<News> implements DataInterface<News> {
      @Id(assignable: true)
      @JsonKey(defaultValue: 0)
      int lid = 0;
    
      @Backlink()
      final Picture = ToMany<NewsPicture>();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多