schultek / stormberry

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

unable to insert data #38

Open sunilmishra opened 1 year ago

sunilmishra commented 1 year ago

Hi,

I am using Dart_Frog, Stormberry, Postgres.

CREATE TABLE Product_Entity (
uid uuid DEFAULT uuid_generate_v4(),
title text NOT NULL, 
description text NOT NULL, 
price numeric NOT NULL, 
category text NOT NULL,
image text,
rating text,
PRIMARY KEY (uid)
);

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

INSERT INTO Product_Entity (title, description, price, category) VALUES ('This is Title 1', 'This is description 1', 2.5, 'sports');
SELECT * from Product_Entity;

Above queries works perfectly. I am using postico2.

//////// NOW ////////////////////

Model class using Stormberry.

@Model(tableName: 'Product_Entity')
abstract class ProductEntity {
  @PrimaryKey()
  String? get uid;
  String get title;
  String get description;
  double get price;
  String get category;
}

After .schema.g.dart generated:

class ProductEntityInsertRequest {
  ProductEntityInsertRequest(
      {this.uid, required this.title, required this.description, required this.price, required this.category});
  String? uid;
  String title;
  String description;
  double price;
  String category;
}

///////// insert query looks like

@override
  Future<void> insert(Database db, List<ProductEntityInsertRequest> requests) async {
    if (requests.isEmpty) return;

    await db.query(
      'INSERT INTO "Product_Entity" ( "uid", "title", "description", "price", "category" )\n'
      'VALUES ${requests.map((r) => '( ${registry.encode(r.uid)}, ${registry.encode(r.title)}, ${registry.encode(r.description)}, ${registry.encode(r.price)}, ${registry.encode(r.category)} )').join(', ')}\n'
      'ON CONFLICT ( "uid" ) DO UPDATE SET "title" = EXCLUDED."title", "description" = EXCLUDED."description", "price" = EXCLUDED."price", "category" = EXCLUDED."category"',
    );
  }

Problem:

When trying to insert data using Dart_Frog as

 final request = ProductEntityInsertRequest(
      title: product.title,
      description: product.description,
      price: product.price,
      category: describeEnum(product.category),
    );
    await database.productEntities.insertOne(request);

It's throw this error:


INSERT INTO "Product_Entity" ( "uid", "title", "description", "price", "category" ) VALUES ( null, 'Product title 1', 'This is Product Description 1', 2.5, 'computers' ) ON CONFLICT ( "uid" ) DO UPDATE SET "title" = EXCLUDED."title", "description" = EXCLUDED."description", "price" = EXCLUDED."price", "category" = EXCLUDED."category" [ERROR] 2022-12-22 23:22:57.797018 0:00:00.013230 POST /products

the issue is InsertQuery trying to insert null value in uid (ie: primaryKey)...

  1. When trying to use UUID as primary key, it's fail.
  2. When trying UUID , generated insertOne does NOT return anything....I guess, It should return uuid.

IDK, how to discard primary key field for InsertQuery, while using uuid_generate_v4 ?

schultek commented 1 year ago

Thanks, i will investigate this.

sunilmishra commented 1 year ago

@schultek any update on this?

schultek commented 1 year ago

I have to add to features for this: uuids as keys and default values. I think currently there isn't really a workaround except providing a uuid manually when inserting.