Different query for android and iOS - Datastore
Describe the bug I have a 1:n relation between a user and his books. Below is the graphql schema
Graphql schema
type User @model {
id: ID!
name: String!
email: String!
premiumTill: AWSDateTime!
books: [Book] @connection(keyName: "byUser", fields: ["id"])
}
type Book @model
@key(name: "byUser", fields: ["userID", "year"]) {
id: ID!
title: String!
year: Int
userID: ID!
user: User @connection(fields: ["userID"])
chapters: [Chapter] @connection(keyName: "byBook", fields: ["id"])
}
I noticed when I want to fetch books by userId, on iOS I have to write the following command:-
this._books = await Amplify.DataStore.query(
Book.classType,
where: Book.USER.eq(AppSettings.userId),
sortBy: [Book.YEAR.descending()],
);
The above code runs fine when I am running my project on an iOS simulator. But when I ran my project on a real android device, it threw me the following error:-
Click to expand!
``` E/SQLiteLog( 5157): (1) no such column: user in "SELECT `Book`.`id` AS `Book_id`, `Book`.`title` AS `Book_title`, `Book`.`year` AS `Book_year`, `Book`.`userID` AS `Book_userID`, `User`.`id` AS `User_id`, `User`.`email` AS `User_email E/amplify:flutter:datastore( 5157): Query operation failed. E/amplify:flutter:datastore( 5157): DataStoreException{message=Error in querying the model., cause=android.database.sqlite.SQLiteException: no such column: user (code 1 SQLITE_ERROR): , while compiling: SELECT `Book`.`id` AS `Book_id`, `Book`.`title` AS `Book_title`, `Book`.`year` AS `Book_year`, `Book`.`userID` AS `Book_userID`, `User`.`id` AS `User_id`, `User`.`email` AS `User_email`, `User`.`name` AS `User_name`, `User`.`premiumTill` AS `User_premiumTill` FROM `Book` LEFT JOIN `User` ON `Book`.`userID`=`User`.`id` WHERE user = ? ORDER BY `Book`.`year` DESC;, recoverySuggestion=See attached exception for details.} E/amplify:flutter:datastore( 5157): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$SQLiteStorageAdapter(SQLiteStorageAdapter.java:437) E/amplify:flutter:datastore( 5157): at com.amplifyframework.datastore.storage.sqlite.-$$Lambda$SQLiteStorageAdapter$YrR7B1Rj3JmC_omOKlzMTn0U5hM.run(Unknown Source:10) E/amplify:flutter:datastore( 5157): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) E/amplify:flutter:datastore( 5157): at java.util.concurrent.FutureTask.run(FutureTask.java:266) E/amplify:flutter:datastore( 5157): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) E/amplify:flutter:datastore( 5157): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) E/amplify:flutter:datastore( 5157): at java.lang.Thread.run(Thread.java:923) E/amplify:flutter:datastore( 5157): Caused by: android.database.sqlite.SQLiteException: no such column: user (code 1 SQLITE_ERROR): , while compiling: SELECT `Book`.`id` AS `Book_id`, `Book`.`title` AS `Book_title`, `Book`.`year` AS `Book_year`, `Book`.`userID` AS `Book_userID`, `User`.`id` AS `User_id`, `User`.`email` AS `User_email`, `User`.`name` AS `User_name`, `User`.`premiumTill` AS `User_premiumTill` FROM `Book` LEFT JOIN `User` ON `Book`.`userID`=`User`.`id` WHERE user = ? ORDER BY `Book`.`year` DESC; E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteProgram.For android, the relation query mentioned in the document https://docs.amplify.aws/lib/datastore/relational/q/platform/flutter#querying-relations works as expected.
this._books = await Amplify.DataStore.query(
Book.classType,
where: User.ID.eq(AppSettings.userId),
sortBy: [Book.YEAR.descending()],
);
But I'm not able to fetch any record on the iOS simulator using the above query. I'm new to Amplify Datastore so kindly clarify how we are supposed to query 1:n relational tables in Datastore which would work on both the platform?
Expected behavior Should have a common way to query results.
pubspec.yaml
amplify_flutter: '<1.0.0'
amplify_auth_cognito: '<1.0.0'
amplify_analytics_pinpoint: '<1.0.0'
amplify_datastore: '<1.0.0'
amplify_api: '<1.0.0'
amplify_storage_s3: '<1.0.0'
Platform This issue is reproducible in both Android and iOS.
Output of flutter doctor -v
[✓] Flutter (Channel stable, 2.0.1, on macOS 11.2.3 20D91 darwin-arm, locale
en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.54.2)
[✓] Connected device (2 available)
• No issues found!
Smartphone (please complete the following information):
- Device: iPhone12 and MI A3
- OS: iOS 13 and Android version 11
- amplify --version -> 4.45.0
Any update?
I also have the same issue working perfectly on the IOS simulator. in android error:
Query failed: DataStoreException(message: Error in querying the model...
I have the same issue. Works in iOS but not in Android.
Error DataStoreException(message: Error in querying the model., recoverySuggestion: See attached exception for details., underlyingException: android.database.sqlite.SQLiteException: no such column: xxxx (code 1 SQLITE_ERROR): , while compiling: SELECT... Sample Data Model:
type Dept @model { id: ID! emps: [Emp] @connection(keyName: "byDeptEmpId", fields: ["id"]) }
type Emp @model { id: ID! empID: ID! dept: Dept! @connection(fields: ["empID"]) empName: String }
Query
List
@Amplifiyer @Ashish-Nanda
Hi all, sorry to getting back on this late! Took a look, it seems a discrepancy between amplify-ios and amplify-android. Take @jobypthomas 's schema as example.
List __emps = await Amplify.DataStore.query(
Emp.classType,
where: Emp.DEPT.eq(dept.id),
);
Emp.DEPT presents a belongsTo relationship. When Flutter passes above predicate into amplify-ios, while generating sql command with predicate, it resolves actual column name from predicate + model schema. With this example, where: Emp.DEPT.eq(dept.id) gets translated to
where empID = ?
In amplify-android, however, it just takes in the predicate as is, and use the filedName presented in the predicate ("dept" in this example) as column name, and causes above mentioned exceptions.
I will investigate a bit more and look for potential solutions.
(reproducible with the latest version of amplify-flutter + amplify-android)
@HuiSF Thank you working on this issue. Do you know when will it be released?
The fix for this issue has been released with amplify-flutter 0.2.2 version. Please give it a try, and feel free to follow up if you encountered any issues. Thanks :)