Problem fetching when primary key is an integer
Hi.
Im trying to fecth a table using a match to the primary key tath is an int but it throw an expection when i try it but if i change the database id to be a biginteger it works.
Here is the exception
PostgreSQLSeverity.error : Specified parameter types do not match column parameter types in query SELECT contenido.id,contenido.titulo,contenido.descripcion,contenido.aparece,contenido.url,contenido.thumbnail FROM contenido WHERE contenido.id = @contenido_id:int8 LIMIT 1 Detail: null Hint: null Table: null Column: null Constraint: null
and this is the controller i use
@httpGet
getContenido(@HTTPPath("id") int id) async{
var q = new Query<Contenido>()
..matchOn.id = whereEqualTo(id);
var u = await q.fetchOne();
if (u == null) {
return new Response.notFound();
}
return new Response.ok(u);
}
Your request controller seems good. Is your primary key in the _Contenido class annotated with @managedPrimaryKey? Can you paste your model object class?
import '../campus.dart';
class Contenido extends ManagedObject<_Contenido> implements _Contenido{}
class _Contenido {
@managedPrimaryKey
@ManagedColumnAttributes(autoincrement: true, unique:true)
int id;
String titulo;
String descripcion;
Formato formato;
Tipo tipo;
Evento evento;
String aparece;
String url;
Plataforma plataforma;
String thumbnail;
static String tableName() {
return "contenido";
}
}
I was unable to reproduce your issue, but I did notice one thing. The @managedPrimaryKey attribute already includes autoincrementing and a unique index, so you don't need the second annotation and in fact for multiple annotations only one is recognized. I think the issue might be that different parts of the system are recognizing a different annotation and its causing the type mismatch. Try removing the ManagedColumnAttributes annotation and let me know if that resolves the issue.
managedPrimaryKey is an instance of ManagedColumnAttributes. There are two competing metadata for the same property. Remove @ManagedColumnAttributes - managedPrimaryKey is already auto-incrementing, and isPrimaryKey infers uniqueness.
FYI, I just bumped into the same issue working with a pre-existing DB. Nothing to do with managedColumnAttributes (I am only using managedPrimaryKey), as the issue title states the error happens if the DB column data type is defined as 'integer'. Simply changing the column to 'bigint' makes this go away. I also tested with a 'smallint' type and had the same error.
This is therefore still an open issue, unless it is simply an Aqueduct requirement that primary key must be 'bigint'? Is that documented somewhere?
@kbirch managedPrimaryKey is a convenience for bigint primary keys that is defined like this:
const ManagedColumnAttributes managedPrimaryKey = const ManagedColumnAttributes(
primaryKey: true,
databaseType: ManagedPropertyType.bigInteger,
autoincrement: true);
You can remove managedPrimaryKey and replace it with a similar construct, but a different type:
class _MyObject {
@ManagedColumnAttributes(primaryKey: true, databaseType: ManagedPropertyType.integer)
int primaryKey;
}
(this is also how you would define a string primary key, for example)