-
Notifications
You must be signed in to change notification settings - Fork 247
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
when querying models with nullable nested models
#3084
Comments
@RobsonT - Could you share your schema (at least the portion that includes the model you are query, and any models it is related to) and the code that you are running that causes this exception? Thanks. |
Hello @Jordan-Nelson , Schema:
Code:
|
I did some tests and managed to run the project. Apparently the error occurs when "subdivisaoID" in "Safra" is null, working correctly when this field is filled. However, this field is marked as "not required" precisely because it does not need a subdivisaoID in some cases. |
Thanks. Can you share the definitions for |
@Jordan-Nelson, These are the models: type Talhao @model @auth(rules: [{allow: public}]) { type Subdivisao @model @auth(rules: [{allow: public}]) { |
@RobsonT - Apologies for the delay in a response. I see you indicated that you are using iOS and Android in the issue. The logs provided are from Android. Can you confirm you are experiencing this issue on iOS? Note: you may need to run the app from xcode to see the native iOS logs. |
Hi @RobsonT - Please let me know if you are still experiencing this issue. |
@Jordan-Nelson, Sorry for the delay. The error occurs in both android and iOS. |
Hi @RobsonT - I was able to create a minimal reproduction of this issue. However, I am only seeing the issue on Android. The exception is not thrown on iOS. Can you confirm you are also seeing this on iOS? Here is the schema, app, and steps I took to reproduce: Repro steps:
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"])
} App import 'package:flutter/material.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_datastore/amplify_datastore.dart';
import 'amplifyconfiguration.dart';
import 'models/ModelProvider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_configureAmplify();
}
Future<void> _configureAmplify() async {
final datastorePlugin = AmplifyDataStore(
modelProvider: ModelProvider.instance,
);
await Amplify.addPlugin(datastorePlugin);
try {
await Amplify.configure(amplifyconfig);
} on AmplifyAlreadyConfiguredException {
safePrint('Amplify already configured.');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('My App'),
actions: [
FilledButton(
onPressed: Amplify.DataStore.clear,
child: const Text('Clear DB'),
),
],
),
body: const Center(
child: Column(
children: [
FilledButton(
onPressed: addPlot,
child: Text('Add Plot'),
),
FilledButton(
onPressed: addCrop,
child: Text('Add Crop'),
),
FilledButton(
onPressed: addCropWithoutSubdivision,
child: Text('Add Crop without Subdivision'),
),
FilledButton(
onPressed: queryCrops,
child: Text('Query Crops'),
),
],
),
),
),
);
}
}
Future<void> addPlot() async {
final talhao = Talhao(nome: 'New Talhao ${uuid()}');
try {
await Amplify.DataStore.save(talhao);
safePrint('New plot saved!');
} on DataStoreException catch (e) {
safePrint('Something went wrong saving model: ${e.message}');
}
}
Future<void> addCrop() async {
final talhao = Talhao(nome: 'New Talhao ${uuid()}');
final subdivisao = Subdivisao(talhao: talhao);
final safra = Safra(talhao: talhao, subdivisao: subdivisao);
try {
await Amplify.DataStore.save(talhao);
await Amplify.DataStore.save(subdivisao);
await Amplify.DataStore.save(safra);
safePrint('New crop saved!');
} on DataStoreException catch (e) {
safePrint('Something went wrong saving model: ${e.message}');
}
}
Future<void> addCropWithoutSubdivision() async {
final talhao = Talhao(nome: 'New Talhao ${uuid()}');
final safra = Safra(talhao: talhao);
try {
await Amplify.DataStore.save(talhao);
await Amplify.DataStore.save(safra);
safePrint('New crop saved!');
} on DataStoreException catch (e) {
safePrint('Something went wrong saving model: ${e.message}');
}
}
Future<void> queryCrops() async {
final talhoes = await Amplify.DataStore.query(Talhao.classType);
for (var talhao in talhoes) {
safePrint('talhao: ${talhao.id}');
final safras = await Amplify.DataStore.query(
Safra.classType,
where: Safra.TALHAO.eq(talhao.id),
);
for (final safra in safras) {
safePrint('safra: ${safra.id}');
}
}
} |
We ran some tests and the error did not actually occur on iOS. We probably confused some mistake we made with this inavlid primary key error. So at the moment it only happens on android. |
I don't know if it's related, but we have an error in another table that only occurs on iOS, apparently also referring to null optional fields.
|
Any updates on this issue? |
@RobsonT - This seems to be an issue that will need to be addressed in Amplify Android. I opened an issue (linked above) in Amplify Android to track this. I don't have a timeline for a fix at the moment. I don't think the other error you have shared is related. If you have reproducible steps for that, can you open a new issue? |
IllegalStateException: Invalid Primary Key
when querying models with nullable nested models
FYI - Amplify Android issue - aws-amplify/amplify-android#2488 |
Hi @RobsonT, the amplify-android team has a PR open with a fix for this issue. After it is released on their library, the bug fix will subsequently be released on amplify-flutter. Thank you for your patience. |
Thank you for your patience. The issue has been fixed in version 1.8.0. and I'm closing it. However, if you encounter any issues after updating to version 1.8.0, please don't hesitate to reopen it. |
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
Categories
Steps to Reproduce
The error occurs in some tables with a 1:n relationship, when making the query filtering by the foreign key.
Error:
E/amplify:flutter:datastore(24262): at java.lang.Thread.run(Thread.java:923) E/amplify:flutter:datastore(24262): 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(24262): at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128) E/amplify:flutter:datastore(24262): at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:900) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:872) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:444) E/amplify:flutter:datastore(24262): ... 6 more E/amplify:flutter:datastore(24262): Query operation failed. E/amplify:flutter:datastore(24262): 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(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:450) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda8.run(Unknown Source:10) E/amplify:flutter:datastore(24262): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) E/amplify:flutter:datastore(24262): at java.util.concurrent.FutureTask.run(FutureTask.java:266) E/amplify:flutter:datastore(24262): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) E/amplify:flutter:datastore(24262): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) E/amplify:flutter:datastore(24262): at java.lang.Thread.run(Thread.java:923) E/amplify:flutter:datastore(24262): 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(24262): at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128) E/amplify:flutter:datastore(24262): at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:900) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:872) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:444) E/amplify:flutter:datastore(24262): ... 6 more E/amplify:flutter:datastore(24262): Query operation failed. E/amplify:flutter:datastore(24262): 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(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:450) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda8.run(Unknown Source:10) E/amplify:flutter:datastore(24262): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) E/amplify:flutter:datastore(24262): at java.util.concurrent.FutureTask.run(FutureTask.java:266) E/amplify:flutter:datastore(24262): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) E/amplify:flutter:datastore(24262): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) E/amplify:flutter:datastore(24262): at java.lang.Thread.run(Thread.java:923) E/amplify:flutter:datastore(24262): 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(24262): at com.amplifyframework.core.model.ModelIdentifier$Helper.getUniqueKey(ModelIdentifier.java:128) E/amplify:flutter:datastore(24262): at com.amplifyframework.core.model.SerializedModel$Builder.serializedData(SerializedModel.java:340) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:900) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.createSerializedModel(SQLiteStorageAdapter.java:872) E/amplify:flutter:datastore(24262): at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$5$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:444) E/amplify:flutter:datastore(24262): ... 6 more
Screenshots
No response
Platforms
Flutter Version
3.3.10
Amplify Flutter Version
0.6.13
Deployment Method
Amplify CLI
Schema
No response
The text was updated successfully, but these errors were encountered: