schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
68 stars 17 forks source link

Cant Update One nor Update Many #48

Closed Rekshot21 closed 1 year ago

Rekshot21 commented 1 year ago

Im having this error when I try to update:
"An exception occurred. PostgreSQLException (PostgreSQLSeverity.error 42883: operator does not exist: integer = text Hint: No operator matches name and types of arguments. Explicit type conversion may need to be added.)"

This is my call:
image

This is my model:
image

I did the migration 4 times already, I tried to workaround this by making a get, delete and insert, but I cant specify the Id in the insert either. Am I doing something wrong?

schultek commented 1 year ago

Did you change the type of a field between int and string? Was the migration successful or failed?

Rekshot21 commented 1 year ago

I didnt change any field from int to string and the migration was succesfull every time.

image

Table: image

schultek commented 1 year ago

Ok I need to test this then. On what version are you?

Rekshot21 commented 1 year ago

stormberry: ^0.10.0

Dart: 2.18.3

Rekshot21 commented 1 year ago

I will add the print when the query runs in case it helps:

Database: connected

UPDATE "users" SET "name" = COALESCE(UPDATED."name"::text, "users"."name"), "age" = COALESCE(UPDATED."age"::int8, "users"."age") FROM ( VALUES ( @0, @1, @2 ) ) AS UPDATED("id", "name", "age") WHERE "users"."id" = UPDATED."id"

Rekshot21 commented 1 year ago

@schultek Hey I was testing this out, it seems the error is when postgres replaces the id parameter it adds the ' and thats why its failing.

So this was the query that was been generated:

UPDATE "users" SET "name" = COALESCE(UPDATED."name"::text, "users"."name"), "age" = COALESCE(UPDATED."age"::int8, "users"."age") FROM ( VALUES ( @0, @1, @2 ) ) AS UPDATED("id", "name", "age") WHERE "users"."id" = UPDATED."id"

The query is okay but when postgress replaces the parameters the @0 goes from the intended int id (for example 1) to a string type (1 will turn into '1').

If I use a String Id it works okay.

Also if I have a field as bigint and postgress does the same thing (for example I try to set a 54 and postgres mades it a '54') it works alright and I have no errors.

I hope being right and that this helps.

Testing made for easier follow up:

Int Id Model
´´´dart @Model() abstract class User { @PrimaryKey() @AutoIncrement() int get id;

int get businessId;

String get userName;

String? get email;

String? get phone;

String get password;

String get role; } ´´´ Generated Query:
´´´

UPDATE "users" SET "business_id" = COALESCE(UPDATED."business_id"::int8, "users"."business_id"), "user_name" = COALESCE(UPDATED."user_name"::text, "users"."user_name"), "email" = COALESCE(UPDATED."email"::text, "users"."email"), "phone" = COALESCE(UPDATED."phone"::text, "users"."phone"), "password" = COALESCE(UPDATED."password"::text, "users"."password"), "role" = COALESCE(UPDATED."role"::text, "users"."role") FROM ( VALUES ( @0, @1, @2, @3, @4, @5, @6 ) ) AS UPDATED("id", "business_id", "user_name", "email", "phone", "password", "role") WHERE "users"."id" = UPDATED."id" ´´´ The Query has the right syntax in PgAdmin4
image

If I input the parameters manually:
image
It Updates no problem:
image

If I set the ID as a String manually I have the error above
image image

It might be another thing but from I was testing this is what I found,

hope this helps

schultek commented 1 year ago

Thanks I think I know now how to fix it.

schultek commented 1 year ago

Should be fixed in v0.11.0

mhmzdev commented 1 year ago

FYI I'm facing this issue in 0.12.1

The error I'm facing is of radix, if I convert it manually to int

FormatException: Invalid radix-16 number (at character 1)

CC: @schultek

schultek commented 1 year ago

@mhmzdev Can you open a new issue for your problem?