Invalid Primary Key, It should either be single field or of type composite primary key
Description
I'm using the datastore to query a table, filtering by a foreign key. when passing the foreign key, some manage to bring the data, but others generate the error: Invalid Primary Key, It should either be single field or of type composite primary key. It works on iOS but it does not work on Android.
This issue is exactly the same issue here. I tried to go back to the fix version mentioned 1.8.0 but it is too old and causing conflicts in package dependancies. I tried different versions: 2.4.0 , 2.5.0 , 2.6.0 and the issue is there in all versions.
Schema attached below.
This is my full log:
E/amplify:flutter:datastore(19325): 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(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$7$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:560)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda7.run(Unknown Source:10)
E/amplify:flutter:datastore(19325): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
E/amplify:flutter:datastore(19325): at java.util.concurrent.FutureTask.run(FutureTask.java:264)
E/amplify:flutter:datastore(19325): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
E/amplify:flutter:datastore(19325): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
E/amplify:flutter:datastore(19325): at java.lang.Thread.run(Thread.java:1012)
E/amplify:flutter:datastore(19325): 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(19325): at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128)
E/amplify:flutter:datastore(19325): at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:990)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$7$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:554)
E/amplify:flutter:datastore(19325): ... 6 more
E/amplify:flutter:datastore(19325): Query operation failed.
E/amplify:flutter:datastore(19325): 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(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$7$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:560)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda7.run(Unknown Source:10)
E/amplify:flutter:datastore(19325): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
E/amplify:flutter:datastore(19325): at java.util.concurrent.FutureTask.run(FutureTask.java:264)
E/amplify:flutter:datastore(19325): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
E/amplify:flutter:datastore(19325): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
E/amplify:flutter:datastore(19325): at java.lang.Thread.run(Thread.java:1012)
E/amplify:flutter:datastore(19325): 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(19325): at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128)
E/amplify:flutter:datastore(19325): at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:990)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:962)
E/amplify:flutter:datastore(19325): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$7$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:554)
E/amplify:flutter:datastore(19325): ... 6 more
E/flutter (19325): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DataStoreException {
E/flutter (19325): "message": "Error in querying the model.",
E/flutter (19325): "recoverySuggestion": "See attached exception for details.",
E/flutter (19325): "underlyingException": "java.lang.IllegalStateException: Invalid Primary Key, It should either be single field or of type composite primary key Primary Key.java.lang.NullPointerException"
E/flutter (19325): }
E/flutter (19325): #0 AmplifyDataStoreMethodChannel.query (package:amplify_datastore/src/method_channel_datastore.dart:147:7)
E/flutter (19325): <asynchronous suspension>
E/flutter (19325): #1 ScheduleService._updateMyScheduleAmplify (package:blink/services/schedule_service.dart:323:26)
E/flutter (19325): <asynchronous suspension>
E/flutter (19325):
E/flutter (19325): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DataStoreException {
E/flutter (19325): "message": "Error in querying the model.",
E/flutter (19325): "recoverySuggestion": "See attached exception for details.",
E/flutter (19325): "underlyingException": "java.lang.IllegalStateException: Invalid Primary Key, It should either be single field or of type composite primary key Primary Key.java.lang.NullPointerException"
E/flutter (19325): }
E/flutter (19325): #0 AmplifyDataStoreMethodChannel.query (package:amplify_datastore/src/method_channel_datastore.dart:147:7)
E/flutter (19325): <asynchronous suspension>
E/flutter (19325): #1 ExperienceAmplifyService.toggleExperienceInMySchedule (package:blink/services/amplify/experience_amplify_service.dart:196:27)
E/flutter (19325): <asynchronous suspension>
E/flutter (19325): #2 AmplifyScheduleService.toggleExperienceInMySchedule (package:blink/services/schedule_service/amplify_schedule_servive.dart:81:5)
E/flutter (19325): <asynchronous suspension>
and this is my query:
final userSchedule = await Amplify.DataStore.query(ModelsProvider.UserSchedule.classType, where: ModelsProvider.UserSchedule.USER.eq(userId));
Categories
- [ ] Analytics
- [ ] API (REST)
- [ ] API (GraphQL)
- [ ] Auth
- [ ] Authenticator
- [x] DataStore
- [ ] Notifications (Push)
- [ ] Storage
Steps to Reproduce
Deploy the schema try to run the query
final userSchedule = await Amplify.DataStore.query(ModelsProvider.UserSchedule.classType, where: ModelsProvider.UserSchedule.USER.eq(userId));
Screenshots
No response
Platforms
- [ ] iOS
- [x] Android
- [ ] Web
- [ ] macOS
- [ ] Windows
- [ ] Linux
Flutter Version
3.22.3
Amplify Flutter Version
2.5.0
Deployment Method
Amplify CLI (Gen 1)
Schema
type UserSchedule @model @auth(rules: [{ allow: public }]) {
id: ID!
session: Session @belongsTo(fields: ["sessionId"])
sessionId: ID! @index(name: "bySession")
user: User @belongsTo(fields: ["userId"])
userId: ID! @index(name: "byUser")
eventId: ID!
}
type User @model @auth(rules: [{ allow: public }]) {
id: ID!
ssoId: String!
email: String!
sxxPressNumbers: Int
settings: String
personID: ID
visitedPlaces: [ID]
scheduleSessions: [UserSchedule] @hasMany(indexName: "byUser", fields: ["id"])
attendedSessions: [UserAttendedSessions] @hasMany(indexName: "byAttendedUser", fields: ["id"])
notifications: [Notifications] @hasMany(indexName: "byNotifications", fields: ["id"])
notes: [Note] @hasMany(indexName: "byUser", fields: ["id"])
gallery: [UploadedImages] @hasMany(indexName: "byUploadedImages", fields: ["id"])
reviewEvent: [ReviewEvent] @hasMany(indexName: "byUser", fields: ["id"])
eventIds: [ID]
}
type Session @model @auth(rules: [{ allow: public }]) {
id: ID!
order: Int
title: String!
subtitle: String
squareImage : String
portraitImage : String
landscapeImage : String
description: String
startDate: AWSDateTime!
endDate: AWSDateTime!
startTimeInMinutes: Int!
endTimeInMinutes: Int!
pollUrl: String
isCanceled: Boolean
isFeatured: Boolean
isPublished: Boolean
isHidden: Boolean
isDeleted: Boolean
isLocked: Boolean
accessibilityChecklist: String # Map (accessibility options)
urls: String # Map (type, link)
eventIds: [ID]!
}
Hello @msaudiblink, thanks for taking the time to open this issue. Since the issue is only occurring on Android it it most likely related to the Android DataStore SDK we are wrapping. We will attempt to reproduce the issue and will transfer this issue once we confirm it's not caused by our Flutter package.