objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

Using or in Query with link #546

Closed mahmoud-araby closed 8 months ago

mahmoud-araby commented 10 months ago

If add a link, backLink in QueryBuilder and add or() in entity condition ( not the linked entity ) returned result using find, findAsync is not accurate following the returned data if think that return is like executing the main condition and the link in two separate functions.

Basic info:

Steps to reproduce

  1. build a query with Query Builder containing a specific condition "equal"
  2. use the link to a ToOne<> and put a very specific condition "equal"
  3. use findAsync() and check data return

Expected behavior

the returned result must be a and between a link and a specific condition as explained in the doc :

// get all Address objects with street "Sesame Street"...
QueryBuilder<Address> builder = 
    addressBox.query(Address_.street.equals('Sesame Street'));
// ...which are linked from a Person named "Elmo"
builder.backlinkMany(Person_.addresses, Person_.name.equals('Elmo'));
Query<Address> query = builder.build();
List<Address> sesameStreetsWithElmo = query.find();
query.close();

but the query returns all results from the link and condition as if they are executed separately.

Code

     QueryBuilder<OrderModelDto> queryBuild= _dbController.box<OrderModelDto>().query(OrderModelDto_.businessDate
            .equals(orderFilter.businessDate!.millisecondsSinceEpoch)
            .or(OrderModelDto_.sessionUid.isNull()));
   queryBuild.link(OrderModelDto_.orderType, OrderTypeDto_.id.equals(5));

      Query<OrderModelDto> query = queryBuild.build();

      List<OrderModelDto> ret = await query.find();

      query.close();
      AppLogger.appLogger( "condition from OrderModelDto  by  ${query.describeParameters()} , ${query.count()}",  name: "Db Controller");

The return data count is greater than the condition itself without a link as if the link is not looking anymore at the condition on OrderModelDto

Dart SDK 3.0.5
Flutter SDK 3.10.5
microtec 1.0.12+21
|-- archive 3.3.7
|   |-- pointycastle 3.7.3
|   |   |-- convert 3.1.1
|   |   |   '-- typed_data...
|   |   |-- collection...
|   |   '-- js...
|   |-- crypto...
|   '-- path...
|-- audioplayers 4.1.0
|   |-- audioplayers_android 3.0.2
|   |   |-- audioplayers_platform_interface...
|   |   '-- flutter...
|   |-- audioplayers_darwin 4.1.0
|   |   |-- audioplayers_platform_interface...
|   |   '-- flutter...
|   |-- audioplayers_linux 2.1.0
|   |   |-- audioplayers_platform_interface...
|   |   '-- flutter...
|   |-- audioplayers_platform_interface 5.0.1
|   |   |-- flutter...
|   |   |-- meta...
|   |   '-- plugin_platform_interface...
|   |-- audioplayers_web 3.1.0
|   |   |-- audioplayers_platform_interface...
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   '-- js...
|   |-- audioplayers_windows 2.0.2
|   |   |-- audioplayers_platform_interface...
|   |   '-- flutter...
|   |-- file 6.1.4
|   |   |-- meta...
|   |   '-- path...
|   |-- http 0.13.6
|   |   |-- async...
|   |   |-- http_parser...
|   |   '-- meta...
|   |-- synchronized 3.1.0
|   |-- flutter...
|   |-- meta...
|   |-- path_provider...
|   '-- uuid...
|-- cached_network_image 3.2.3
|   |-- cached_network_image_platform_interface 2.0.0
|   |   |-- flutter...
|   |   '-- flutter_cache_manager...
|   |-- cached_network_image_web 1.0.2
|   |   |-- cached_network_image_platform_interface...
|   |   |-- flutter...
|   |   '-- flutter_cache_manager...
|   |-- flutter_cache_manager 3.3.1
|   |   |-- sqflite 2.2.8+4
|   |   |   |-- sqflite_common 2.4.5+1
|   |   |   |   |-- meta...
|   |   |   |   |-- path...
|   |   |   |   '-- synchronized...
|   |   |   |-- flutter...
|   |   |   '-- path...
|   |   |-- clock...
|   |   |-- collection...
|   |   |-- file...
|   |   |-- flutter...
|   |   |-- http...
|   |   |-- path...
|   |   |-- path_provider...
|   |   |-- rxdart...
|   |   '-- uuid...
|   |-- octo_image 1.0.2
|   |   |-- flutter_blurhash 0.7.0
|   |   |   '-- flutter...
|   |   '-- flutter...
|   '-- flutter...
|-- carousel_slider 4.2.1
|   '-- flutter...
|-- cherry_toast 1.3.1
|   '-- flutter...
|-- cupertino_icons 1.0.5
|-- dartz 0.10.1
|-- dbcrypt 2.0.0
|-- desktop_multi_window 0.2.0
|   '-- flutter...
|-- device_info_plus 9.0.2
|   |-- device_info_plus_platform_interface 7.0.0
|   |   |-- flutter...
|   |   |-- meta...
|   |   '-- plugin_platform_interface...
|   |-- win32 5.0.5
|   |   '-- ffi...
|   |-- win32_registry 1.1.1
|   |   |-- ffi...
|   |   '-- win32...
|   |-- ffi...
|   |-- file...
|   |-- flutter...
|   |-- flutter_web_plugins...
|   '-- meta...
|-- dio 5.2.1+1
|   |-- http_parser 4.0.2
|   |   |-- collection...
|   |   |-- source_span...
|   |   |-- string_scanner...
|   |   '-- typed_data...
|   |-- async...
|   |-- meta...
|   '-- path...
|-- flutter 0.0.0
|   |-- characters 1.3.0
|   |-- collection 1.17.1
|   |-- js 0.6.7
|   |   '-- meta...
|   |-- material_color_utilities 0.2.0
|   |-- meta 1.9.1
|   |-- sky_engine 0.0.99
|   '-- vector_math 2.1.4
|-- flutter_barcode_listener 0.1.3
|   '-- flutter...
|-- flutter_barcode_scanner 2.0.0
|   |-- flutter_plugin_android_lifecycle 2.0.15
|   |   '-- flutter...
|   '-- flutter...
|-- flutter_bloc 8.1.3
|   |-- bloc 8.1.2
|   |   '-- meta...
|   |-- provider 6.0.5
|   |   |-- nested 1.0.0
|   |   |   '-- flutter...
|   |   |-- collection...
|   |   '-- flutter...
|   '-- flutter...
|-- flutter_local_notifications 15.1.0+1
|   |-- flutter_local_notifications_linux 4.0.0+1
|   |   |-- dbus 0.7.8
|   |   |   |-- args...
|   |   |   |-- ffi...
|   |   |   |-- meta...
|   |   |   '-- xml...
|   |   |-- xdg_directories 0.2.0+3
|   |   |   |-- process 4.2.4
|   |   |   |   |-- file...
|   |   |   |   |-- path...
|   |   |   |   '-- platform...
|   |   |   |-- meta...
|   |   |   '-- path...
|   |   |-- ffi...
|   |   |-- flutter...
|   |   |-- flutter_local_notifications_platform_interface...
|   |   '-- path...
|   |-- flutter_local_notifications_platform_interface 7.0.0+1
|   |   |-- flutter...
|   |   '-- plugin_platform_interface...
|   |-- timezone 0.9.2
|   |   '-- path...
|   |-- clock...
|   '-- flutter...
|-- flutter_localizations 0.0.0
|   |-- characters...
|   |-- clock...
|   |-- collection...
|   |-- flutter...
|   |-- intl...
|   |-- js...
|   |-- material_color_utilities...
|   |-- meta...
|   |-- path...
|   '-- vector_math...
|-- flutter_native_splash 2.3.1
|   |-- args 2.4.2
|   |-- flutter_web_plugins 0.0.0
|   |   |-- characters...
|   |   |-- collection...
|   |   |-- flutter...
|   |   |-- js...
|   |   |-- material_color_utilities...
|   |   |-- meta...
|   |   '-- vector_math...
|   |-- html 0.15.4
|   |   |-- csslib 1.0.0
|   |   |   '-- source_span...
|   |   '-- source_span 1.9.1
|   |       |-- term_glyph 1.2.1
|   |       |-- collection...
|   |       '-- path...
|   |-- image 4.0.17
|   |   |-- archive...
|   |   |-- meta...
|   |   '-- xml...
|   |-- universal_io 2.2.2
|   |   |-- typed_data 1.3.2
|   |   |   '-- collection...
|   |   |-- collection...
|   |   '-- meta...
|   |-- xml 6.3.0
|   |   |-- petitparser 5.4.0
|   |   |   '-- meta...
|   |   |-- collection...
|   |   '-- meta...
|   |-- yaml 3.1.2
|   |   |-- string_scanner 1.2.0
|   |   |   '-- source_span...
|   |   |-- collection...
|   |   '-- source_span...
|   |-- flutter...
|   |-- js...
|   |-- meta...
|   '-- path...
|-- flutter_screenutil 5.8.4
|   '-- flutter...
|-- flutter_staggered_grid_view 0.6.2
|   '-- flutter...
|-- flutter_svg 2.0.7
|   |-- vector_graphics 1.1.7
|   |   |-- flutter...
|   |   '-- vector_graphics_codec...
|   |-- vector_graphics_codec 1.1.7
|   |-- vector_graphics_compiler 1.1.7
|   |   |-- path_parsing 1.0.1
|   |   |   |-- meta...
|   |   |   '-- vector_math...
|   |   |-- args...
|   |   |-- meta...
|   |   |-- vector_graphics_codec...
|   |   '-- xml...
|   '-- flutter...
|-- fluttertoast 8.2.2
|   |-- flutter...
|   '-- flutter_web_plugins...
|-- font_awesome_flutter 10.5.0
|   '-- flutter...
|-- freezed_annotation 2.2.0
|   |-- collection...
|   |-- json_annotation...
|   '-- meta...
|-- get_it 7.6.0
|   |-- async 2.11.0
|   |   |-- collection...
|   |   '-- meta...
|   '-- collection...
|-- hotkey_manager 0.1.8
|   |-- collection...
|   |-- flutter...
|   '-- uuid...
|-- injectable 2.1.2
|   '-- get_it...
|-- internet_connection_checker 1.0.0+1
|-- intl 0.18.0
|   |-- clock 1.1.1
|   |-- meta...
|   '-- path...
|-- json_annotation 4.8.1
|   '-- meta...
|-- objectbox 2.1.0
|   |-- ffi 2.0.2
|   |-- flat_buffers 2.0.5
|   |-- collection...
|   |-- meta...
|   '-- path...
|-- objectbox_flutter_libs 2.1.0
|   |-- objectbox...
|   '-- path_provider...
|-- open_file 3.3.2
|   |-- ffi...
|   '-- flutter...
|-- package_info_plus 4.0.2
|   |-- package_info_plus_platform_interface 2.0.1
|   |   |-- flutter...
|   |   |-- meta...
|   |   '-- plugin_platform_interface...
|   |-- ffi...
|   |-- flutter...
|   |-- flutter_web_plugins...
|   |-- http...
|   |-- meta...
|   |-- path...
|   '-- win32...
|-- path 1.8.3
|-- path_provider 2.0.15
|   |-- path_provider_android 2.0.27
|   |   |-- flutter...
|   |   '-- path_provider_platform_interface...
|   |-- path_provider_foundation 2.2.3
|   |   |-- flutter...
|   |   '-- path_provider_platform_interface...
|   |-- path_provider_linux 2.1.11
|   |   |-- ffi...
|   |   |-- flutter...
|   |   |-- path...
|   |   |-- path_provider_platform_interface...
|   |   '-- xdg_directories...
|   |-- path_provider_platform_interface 2.0.6
|   |   |-- platform 3.1.0
|   |   |-- flutter...
|   |   '-- plugin_platform_interface...
|   |-- path_provider_windows 2.1.7
|   |   |-- ffi...
|   |   |-- flutter...
|   |   |-- path...
|   |   |-- path_provider_platform_interface...
|   |   '-- win32...
|   '-- flutter...
|-- payment_terminals 0.0.1
|   |-- flutter...
|   '-- plugin_platform_interface...
|-- permission_handler 10.4.2
|   |-- permission_handler_android 10.3.0
|   |   |-- flutter...
|   |   '-- permission_handler_platform_interface...
|   |-- permission_handler_apple 9.1.2
|   |   |-- flutter...
|   |   '-- permission_handler_platform_interface...
|   |-- permission_handler_platform_interface 3.11.0+1
|   |   |-- flutter...
|   |   |-- meta...
|   |   '-- plugin_platform_interface...
|   |-- permission_handler_windows 0.1.3
|   |   |-- flutter...
|   |   '-- permission_handler_platform_interface...
|   |-- flutter...
|   '-- meta...
|-- platform_device_id 1.0.1
|   |-- device_info 2.0.3
|   |   |-- device_info_platform_interface 2.0.1
|   |   |   |-- flutter...
|   |   |   |-- meta...
|   |   |   '-- plugin_platform_interface...
|   |   '-- flutter...
|   |-- platform_device_id_linux 1.0.0
|   |   '-- flutter...
|   |-- platform_device_id_macos 1.0.0
|   |   '-- flutter...
|   |-- platform_device_id_platform_interface 1.0.0
|   |   |-- flutter...
|   |   '-- plugin_platform_interface...
|   |-- platform_device_id_web 1.0.0
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   '-- platform_device_id_platform_interface...
|   |-- platform_device_id_windows 1.0.0
|   |   '-- flutter...
|   '-- flutter...
|-- platform_uuid 0.0.1
|   |-- plugin_platform_interface 2.1.4
|   |   '-- meta...
|   '-- flutter...
|-- pos_printer_manager 0.0.5
|   |-- easy_logger 0.0.2
|   |   '-- flutter...
|   |-- esc_pos_utils_plus 2.0.2
|   |   |-- hex 0.2.0
|   |   |-- flutter...
|   |   |-- html...
|   |   '-- image...
|   |-- flutter_usb_printer 0.1.0+1
|   |   '-- flutter...
|   |-- network_info_plus 4.0.1
|   |   |-- network_info_plus_platform_interface 1.1.3
|   |   |   |-- flutter...
|   |   |   |-- meta...
|   |   |   '-- plugin_platform_interface...
|   |   |-- nm 0.5.0
|   |   |   '-- dbus...
|   |   |-- collection...
|   |   |-- ffi...
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   |-- meta...
|   |   '-- win32...
|   |-- ffi...
|   |-- flutter...
|   |-- printing...
|   '-- win32...
|-- presentation_displays 0.2.3
|   '-- flutter...
|-- printing 5.11.0
|   |-- pdf 3.10.4
|   |   |-- barcode 2.2.4
|   |   |   |-- qr 3.0.1
|   |   |   |   '-- meta...
|   |   |   '-- meta...
|   |   |-- bidi 2.0.6
|   |   |-- archive...
|   |   |-- crypto...
|   |   |-- image...
|   |   |-- meta...
|   |   |-- path_parsing...
|   |   |-- vector_math...
|   |   '-- xml...
|   |-- ffi...
|   |-- flutter...
|   |-- flutter_web_plugins...
|   |-- http...
|   |-- image...
|   |-- js...
|   |-- meta...
|   '-- plugin_platform_interface...
|-- reorderable_grid 1.0.6
|   '-- flutter...
|-- sentry_flutter 7.8.0
|   |-- sentry 7.8.0
|   |   |-- http...
|   |   |-- meta...
|   |   |-- stack_trace...
|   |   '-- uuid...
|   |-- flutter...
|   |-- flutter_web_plugins...
|   |-- meta...
|   '-- package_info_plus...
|-- share_plus 7.0.2
|   |-- cross_file 0.3.3+4
|   |   |-- js...
|   |   '-- meta...
|   |-- mime 1.0.4
|   |-- share_plus_platform_interface 3.2.1
|   |   |-- cross_file...
|   |   |-- flutter...
|   |   |-- meta...
|   |   |-- mime...
|   |   |-- path_provider...
|   |   |-- plugin_platform_interface...
|   |   '-- uuid...
|   |-- ffi...
|   |-- file...
|   |-- flutter...
|   |-- flutter_web_plugins...
|   |-- meta...
|   |-- url_launcher_linux...
|   |-- url_launcher_platform_interface...
|   |-- url_launcher_web...
|   |-- url_launcher_windows...
|   '-- win32...
|-- shared_preferences 2.2.0
|   |-- shared_preferences_android 2.2.0
|   |   |-- flutter...
|   |   '-- shared_preferences_platform_interface...
|   |-- shared_preferences_foundation 2.3.0
|   |   |-- flutter...
|   |   '-- shared_preferences_platform_interface...
|   |-- shared_preferences_linux 2.3.0
|   |   |-- file...
|   |   |-- flutter...
|   |   |-- path...
|   |   |-- path_provider_linux...
|   |   |-- path_provider_platform_interface...
|   |   '-- shared_preferences_platform_interface...
|   |-- shared_preferences_platform_interface 2.3.0
|   |   |-- flutter...
|   |   '-- plugin_platform_interface...
|   |-- shared_preferences_web 2.2.0
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   '-- shared_preferences_platform_interface...
|   |-- shared_preferences_windows 2.3.0
|   |   |-- file...
|   |   |-- flutter...
|   |   |-- path...
|   |   |-- path_provider_platform_interface...
|   |   |-- path_provider_windows...
|   |   '-- shared_preferences_platform_interface...
|   '-- flutter...
|-- shelf_web_socket 1.0.4
|   |-- shelf 1.4.1
|   |   |-- stack_trace 1.11.0
|   |   |   '-- path...
|   |   |-- async...
|   |   |-- collection...
|   |   |-- http_parser...
|   |   |-- path...
|   |   '-- stream_channel...
|   |-- stream_channel 2.1.1
|   |   '-- async...
|   '-- web_socket_channel 2.4.0
|       |-- async...
|       |-- crypto...
|       '-- stream_channel...
|-- stop_watch_timer 2.0.0
|   |-- rxdart 0.27.7
|   '-- flutter...
|-- sunmi_printer_plus 2.1.01
|   '-- flutter...
|-- url_launcher 6.1.12
|   |-- url_launcher_android 6.0.36
|   |   |-- flutter...
|   |   '-- url_launcher_platform_interface...
|   |-- url_launcher_ios 6.1.4
|   |   |-- flutter...
|   |   '-- url_launcher_platform_interface...
|   |-- url_launcher_linux 3.0.5
|   |   |-- flutter...
|   |   '-- url_launcher_platform_interface...
|   |-- url_launcher_macos 3.0.5
|   |   |-- flutter...
|   |   '-- url_launcher_platform_interface...
|   |-- url_launcher_platform_interface 2.1.3
|   |   |-- flutter...
|   |   '-- plugin_platform_interface...
|   |-- url_launcher_web 2.0.17
|   |   |-- flutter...
|   |   |-- flutter_web_plugins...
|   |   '-- url_launcher_platform_interface...
|   |-- url_launcher_windows 3.0.6
|   |   |-- flutter...
|   |   '-- url_launcher_platform_interface...
|   '-- flutter...
|-- uuid 3.0.7
|   '-- crypto 3.0.3
|       '-- typed_data...
'-- win_toast 0.1.1
    '-- flutter...
[√] Flutter (Channel stable, 3.10.5, on Microsoft Windows [Version 10.0.22621.1848], locale ar-EG)
    • Flutter version 3.10.5 on channel stable at C:\flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (4 weeks ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at C:\Users\Microtec-D7\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.6.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.6.33801.468
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] VS Code (version 1.79.2)
    • VS Code at C:\Users\Microtec-D7\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22621.1848]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 114.0.5735.199
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 114.0.1823.67

[√] Network resources
    • All expected network resources are available.

• No issues found!
mahmoud-araby commented 10 months ago

after trying to test many cases I think the issue is in using or it, not the isNull()

     QueryBuilder<OrderModelDto> queryBuild= _dbController.box<OrderModelDto>().query(OrderModelDto_.businessDate
            .equals(orderFilter.businessDate!.millisecondsSinceEpoch)
            .or(OrderModelDto_.sessionUid.isNull()));
   queryBuild.link(OrderModelDto_.orderType, OrderTypeDto_.id.equals(5));
 QueryBuilder<OrderModelDto> queryBuild= _dbController.box<OrderModelDto>().query(OrderModelDto_.businessDate
            .equals(orderFilter.businessDate!.millisecondsSinceEpoch)
            .or(OrderModelDto_.sessionUid.equal("notFound")));
   queryBuild.link(OrderModelDto_.orderType, OrderTypeDto_.id.equals(5));

both code samples above returned the same result

I also tried to use a single condition with link and isNull and the result was correct

 QueryBuilder<OrderModelDto> queryBuild= _dbController.box<OrderModelDto().query(OrderModelDto_.sessionUid.isNull());
   queryBuild.link(OrderModelDto_.orderType, OrderTypeDto_.id.equals(5));
greenrobot-team commented 9 months ago

Thanks for reporting. However, without knowing what data you are working with it's hard to guess what you expect to be the correct result.

Maybe note that "link" conditions are always added with "and".

If that does not clear things up, please share a small example project to help me understand your issue better.

mahmoud-araby commented 9 months ago

Thanks for replaying. I have created a small example project with the case

https://github.com/mahmoud-araby/object_boc_relation_test

Steps To Reproduce : 1- Download the Source Code and run it on any platform (Windows, Android, IOS). 2- Project Have a Single Page With One Query (Total . OR Discount ) Link User. Name. 3- Try to put values in the TextFields and Reult Will Pop on the Screen. 4- You can find the initial data in /assets/data.json.

Try to Put Total And Discount more significant than the data in Tables with Specific User Name but Query still returns all data with Link to the User Name. image

greenrobot-team commented 9 months ago

Thanks! I can confirm that when adding the link condition it appears the actual conditions are ignored completely.

Log output:

[Query Describe Parameters] (total >= 20.000000
 OR discount >= 3.000000)
| Link User via userId with conditions: name contains "1"
[Query Describe] Query for entity UserOrder with 3 conditions with properties total, discount, total with 1 links

We'll look into this.

greenrobot-team commented 8 months ago

We have released version 2.3.0 which resolves this issue. Please update using flutter pub upgrade (or dart pub upgrade for Dart Native projects).

Thanks again!