Skip to content

Inability to setParameter or ordered by Id field #51

@singlakarun

Description

@singlakarun

This is pretty coming scenario to execute queries on Id field

// objectbox: entity
class MyEntity {
    var id: Id = 0
//more fields
}

let box: Box<MyEntity> = ...
let query = box.query {
     MyEntity.id.isEquals(to: 0)
}.ordered(by: MyEntity.id) --> COMPILATION ERROR here
.build()

query.setParameter(MyEntity.id, 0) --> COMPILATION ERROR here

Activity

greenrobot

greenrobot commented on Jun 7, 2021

@greenrobot
Member

Some people say IDs should not used in queries at all as they should not represent any application data. What's your use case?

singlakarun

singlakarun commented on Jun 7, 2021

@singlakarun
Author

I am storing chat messages in a table and wanted to extract the last message inserted. Doing it via timestamp might be inaccurate as theoretically there could be multiple messages generated in a millisecond. Only way out is to use my own auto increment column which I want to avoid.
Also I wanted to sort the messages in the reverse insertion order.

greenrobot

greenrobot commented on Jun 7, 2021

@greenrobot
Member

Yes, IDs "happen" to work for that local case. Technically, we don't give guarantees that this will stay like this forever, and, e.g. if you'd use Sync, it would break right away.

Other than that, we'll look into the compilation issue, of course. It will take some time though, as there are other things on our priority queue.

greenrobot-team

greenrobot-team commented on Aug 7, 2024

@greenrobot-team
Member

we'll look into the compilation issue

This is because ordered(by) and setParameter() do not support Id properties (they are restricted to Property<ObjectBox.QueryBuilder<E>.EntityType, T, Void>, but for Id properties the last type parameter is not Void).

The methods would have to be changed to explicitly allow using Id properties. So changing this into an enhancement suggestion.

added
enhancementNew feature or request
and removed
bugSomething isn't working
on Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @greenrobot@greenrobot-team@singlakarun

        Issue actions

          Inability to setParameter or ordered by Id field · Issue #51 · objectbox/objectbox-swift