efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Column "universal_product_code" is of type upc but expression is of type text

Open phristov opened this issue 3 years ago • 1 comments

I am trying to save the string 033200011309 as UPC

PostgreSQL 15.3 Npgsql.EntityFrameworkCore.PostgreSQL 7.0.4

Error

 Exception data:
    Severity: ERROR
    SqlState: 42804
    MessageText: column "universal_product_code" is of type upc but expression is of type text
    Hint: You will need to rewrite or cast the expression.
    Position: 241
    File: parse_target.c
    Line: 587
    Routine: transformAssignedExpr

Product Class

[Column(TypeName = "upc")]
public string UniversalProductCode { get; set; }

Controller

var entity = new Product();
entity.UniversalProductCode = dto.UniversalProductCode;

this.DbContext.Products.Add(entity);
await this.DbContext.SaveChangesAsync();

The following SQL code is working fine.

create table test (
    upc upc not null
);

insert into test (upc) values ('033200011309');

phristov avatar Jul 17 '23 05:07 phristov

The upc type is part of the PG isn extension, which Npgsql doesn't support. The main problem is that the isn types don't support PG binary encoding - only text - but Npgsql generally works with binary encoding. Although upc may look (and behave) like just a text value, it's its own type and can't be written via just text.

I'm going to go ahead and close this for now, since there's not much we can do about it at the moment. To help make isn available with Npgsql, binary support needs to be pushed forward on the PostgreSQL side.

roji avatar Jul 17 '23 07:07 roji