amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Different query for android and iOS - Datastore

Open gauravdangi opened this issue 5 years ago • 8 comments

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.(SQLiteProgram.java:61) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1545) E/amplify:flutter:datastore( 5157): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1484) E/amplify:flutter:datastore( 5157): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.getQueryAllCursor(SQLiteStorageAdapter.java:912) E/amplify:flutter:datastore( 5157): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$SQLiteStorageAdapter(SQLiteStorageAdapter.java:393) E/amplify:flutter:datastore( 5157): ... 6 more I/flutter ( 5157): Delete failed: DataStoreException(message: Error in querying the model., recoverySuggestion: See attached exception for details., underlyingException: 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;) D/AutoSessionTracker( 5157): Activity paused: MainActivity D/AutoSessionTracker( 5157): Activity stopped: MainActivity D/AutoSessionTracker( 5157): Activity state saved: MainActivity ```

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

gauravdangi avatar Mar 15 '21 12:03 gauravdangi

Any update?

gauravdangi avatar Mar 30 '21 07:03 gauravdangi

I also have the same issue working perfectly on the IOS simulator. in android error:

Query failed: DataStoreException(message: Error in querying the model...

yathu avatar Jun 07 '21 11:06 yathu

I have the same issue. Works in iOS but not in Android.

jobypthomas avatar Jul 26 '21 13:07 jobypthomas

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 __emps = await Amplify.DataStore.query( Emp.classType, where: Emp.DEPT.eq(dept.id), );

jobypthomas avatar Jul 26 '21 13:07 jobypthomas

@Amplifiyer @Ashish-Nanda

jobypthomas avatar Jul 27 '21 13:07 jobypthomas

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 avatar Jul 27 '21 19:07 HuiSF

@HuiSF Thank you working on this issue. Do you know when will it be released?

jobypthomas avatar Jul 30 '21 14:07 jobypthomas

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 :)

HuiSF avatar Aug 06 '21 15:08 HuiSF