ToMany 关系在 link() 处给出类型错误

问题描述

我有桌子:

    @Entity()
    @JsonSerializable()
    class DateTimeStruct {
      @JsonKey(ignore: true)
      int id = 0;
      DateTime time = DateTime.Now();
      String? title;
    
      DateTimeStruct();
      factory DateTimeStruct.fromJson(Map<String,dynamic> json) => _$DateTimeStructFromJson(json);
      Map<String,dynamic> toJson() => _$DateTimeStructToJson(this);
    }

    @JsonSerializable(explicitToJson: true)
    @Entity()
    class Event {
      final Time = ToMany<DateTimeStruct>();
    }

当我尝试使用链接查询相关表时,如下所示:

    Store.Box<Event>().query().link(Event_.Time,DateTimeStruct_.time.greaterOrEqual(DateTime.Now().millisecondsSinceEpoch));

我明白了

The argument type 'QueryRelationMany<Event,DateTimeStruct>' can't be assigned to the parameter type 'QueryRelationProperty<Event,DateTimeStruct>'

错误信息。

第二个问题:DateTimeStruct_.time 解析为 QueryIntegerProperty<DateTimeStruct> time,但它是 DateTime。是否在数据库中转换为整数?

解决方法

  1. 给定实体不适用于 pub run build_runner build,因为 Event 缺少 id
  2. 您正在使用 ToMany 关系 - 如果您想为每个 DateTimeStruct 存储多个 Event,您只想这样做 - 只是确保您知道。如果每个事件只有一次没有问题(从概念上讲,这对我来说是正确的),请使用 ToOne
  3. 对于 ToMany,您需要使用 .linkMany() 而不是 .link() 进行链接 - 这就是编译错误的来源。
  4. 是 Dart DateTime 对象存储为毫秒时间戳(除非您使用 @Property(type: PropertyType.dateNano) annotation 另行指定)。您需要这样查询它们 - 您似乎正确地这样做了。