objectbox / objectbox-swift

Swift database - fast, simple and lightweight (iOS, macOS)
https://swift.objectbox.io
Apache License 2.0
452 stars 30 forks source link

1.6.0 automatically generating ID error #54

Closed wangnan1990 closed 1 month ago

wangnan1990 commented 2 years ago

When I use objectBox to auto-generate for entities that don't have any associated relationships, the information about IDs looks like this, Property<UserInfo, Id, Id>, and the documentation, shows that it should be generated correctly as Property<UserInfo, Id, Void>, so when I build a query statement about IDs, I am prompted with the incorrect type.

This is my entity:

// objectbox: entity
public class UserInfo {

    // objectbox: id = {"assignable":true}
    public var entityId: Id = 0;

    public var name: String?;

    public var nick: String?;

}

The following is the automatically generated code:

     /// Generated entity property information.
    ///
    /// You may want to use this in queries to specify fetch conditions, for example:
    ///
    ///     box.query { UserInfo.entityId == myId }
    public static var entityId: Property<UserInfo, Id, Id> { return Property<UserInfo, Id, Id>(propertyId: 1, isPrimaryKey: true) }
    /// Generated entity property information.
    ///
    /// You may want to use this in queries to specify fetch conditions, for example:
    ///
    ///     box.query { UserInfo.name.startsWith("X") }
    public static var name: Property<UserInfo, String?, Void> { return Property<UserInfo, String?, Void>(propertyId: 2, isPrimaryKey: false) }
    /// Generated entity property information.
    ///
    /// You may want to use this in queries to specify fetch conditions, for example:
    ///
    ///     box.query { UserInfo.nick.startsWith("X") }
    public static var nick: Property<UserInfo, String?, Void> { return Property<UserInfo, String?, Void>(propertyId: 3, isPrimaryKey: false) }
    /// Generated entity property information.

And the "Property" is:

    public struct Property<E, V, R> where E : ObjectBox.EntityInspectable, E : ObjectBox.__EntityRelatable, E == E.EntityBindingType.EntityType, V : ObjectBox.EntityPropertyTypeConvertible {

        /// Entity type that contains the property this object is describing.
        public typealias EntityType = E

        /// Supported property value type this is describing.
        public typealias ValueType = V

        /// If this is a ToOne property, this is the type it refers to. Otherwise Void.
        public typealias ReferencedType = R

        public init(propertyId: UInt32, isPrimaryKey: Bool = false)

        /// Indicates if the property is the entity's primary key.
        public var isPrimaryKey: Bool { get }

        /// The internal ID of the property, in terms of the database schema.
        public var propertyId: UInt32 { get }
  }

So, when I use to build anbout “ID” query, It is always wrong.

greenrobot-team commented 1 month ago

The generated code has changed in the meantime, for Id properties it should, for example, now look like:

internal static var id: Property<BusRoute, EntityId<BusRoute>, EntityId<BusRoute>> { return Property<BusRoute, EntityId<BusRoute>, EntityId<BusRoute>>(propertyId: 1, isPrimaryKey: true) }

Closing this issue due to inactivity. :zzz: Feel free to comment with more details or submit a new issue.