Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException: Invalid Primary Key, It should either be single field or of type composite primary key #2488

Closed
1 task done
Jordan-Nelson opened this issue Jun 23, 2023 · 3 comments
Assignees
Labels
bug Something isn't working datastore DataStore category/plugins

Comments

@Jordan-Nelson
Copy link
Member

Before opening, please confirm:

Language and Async Model

Kotlin

Amplify Categories

DataStore

Gradle script dependencies

// Put output below this line

Environment information

com.amplifyframework:aws-datastore:2.4.1

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

An issue was opened in Amplify Flutter where a user is seeing an exception when running a query with a specific schema. I have created a minimal reproduction of this issue (in flutter). The same schema/query seems to work as expected on iOS (based on my testing).

Reproduction steps (if applicable)

  1. Set up an example DS app with the schema below (can be local only, API is not required)
  2. Add a "Safra" model with a "Talhao" and "Subdivisao"
  3. Query "Safra" models by the "Talhao" ID using a where filter
  4. Observe that the query works
  5. Add a "Safra" model with a "Talhao" but no "Subdivisao"
  6. Query "Safra" models by the "Talhao" ID of the new model using a where filter
  7. Observe that the query throws an exception

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line

E/amplify:flutter:datastore( 5116): DataStoreException{message=Error in querying the model., cause=java.lang.IllegalStateException: Invalid Primary Key, It should either be single field or of type composite primary key Primary Key.java.lang.NullPointerException, recoverySuggestion=See attached exception for details.}
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:450)
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda6.run(Unknown Source:10)
E/amplify:flutter:datastore( 5116): 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
E/amplify:flutter:datastore( 5116): 	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
E/amplify:flutter:datastore( 5116): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
E/amplify:flutter:datastore( 5116): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
E/amplify:flutter:datastore( 5116): 	at java.lang.Thread.run(Thread.java:1012)
E/amplify:flutter:datastore( 5116): Caused by: java.lang.IllegalStateException: Invalid Primary Key, It should either be single field or of type composite primary key Primary Key.java.lang.NullPointerException
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128)
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340)
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:900)
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:872)
E/amplify:flutter:datastore( 5116): 	at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:444)
E/amplify:flutter:datastore( 5116): 	... 6 more

amplifyconfiguration.json

No response

GraphQL Schema

input AMPLIFY {
  globalAuthRule: AuthRule = { allow: public }
} # FOR TESTING ONLY!
# Crop
type Safra @model {
  id: ID!
  talhaoID: ID! @index(name: "byTalhao")
  subdivisaoID: ID @index(name: "bySubdivisao")
  talhao: Talhao @belongsTo(fields: ["talhaoID"])
  subdivisao: Subdivisao @belongsTo(fields: ["subdivisaoID"])
}

# Plot
type Talhao @model {
  id: ID!
  nome: String
  Subdivisaos: [Subdivisao] @hasMany(indexName: "byTalhao", fields: ["id"])
  Safras: [Safra] @hasMany(indexName: "byTalhao", fields: ["id"])
}

# Subdivision
type Subdivisao @model {
  id: ID!
  talhaoID: ID! @index(name: "byTalhao")
  Safras: [Safra] @hasMany(indexName: "bySubdivisao", fields: ["id"])
  talhao: Talhao @belongsTo(fields: ["talhaoID"])
}


Additional information and screenshots

No response

@eeatonaws eeatonaws added bug Something isn't working datastore DataStore category/plugins labels Jun 27, 2023
@tylerjroach
Copy link
Member

Thank you for the report. I've verified the IllegalStateException happens on the query method exposed for Flutter, but even the native query method does not return data exactly as expected in this scenario.

Will continue looking into the fix.

@ankpshah
Copy link
Contributor

This has been fixed in version 2.14.11

Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datastore DataStore category/plugins
Projects
None yet
Development

No branches or pull requests

4 participants