Skip to content
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

unable to insert data #38

Open
sunilmishra opened this issue Dec 23, 2022 · 3 comments
Open

unable to insert data #38

sunilmishra opened this issue Dec 23, 2022 · 3 comments

Comments

@sunilmishra
Copy link

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
Copy link
Owner

Thanks, i will investigate this.

@sunilmishra
Copy link
Author

@schultek any update on this?

@schultek
Copy link
Owner

schultek commented Jan 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants