Skip to content

Lack a default Type Mapping for short array in composite to PostgreSQL smallint[]  #5591

Description

@guderianfyzz

Hi,
First, thanks for providing such a great project!

I've encountered a situation where Npgsql seems to lack a default type mapping for short arrays to Postgre smallint[] fields within composite types. Below are the details:

Steps to reproduce

using Npgsql;

// 1. Assuming a PostgreSQL setup with the following type and table:
CREATE TYPE public.inventory_item AS
(
    name text,
    supplier_id integer,
    price numeric,
    shortlist smallint[]
);

CREATE TABLE some_table (
  id serial PRIMARY KEY,
  my_composite inventory_item
);

// 2. C# code that demonstrates the issue:
public class InventoryItem
{
    public string Name { get; set; } = "";
    public int SupplierId { get; set; }
    public decimal Price { get; set; }
    public short[] ShortList { get; set; }
}

// (Code for connection setup using NpgsqlDataSourceBuilder)

await using var conn = dataSource.CreateConnection();
await conn.OpenAsync();

await using var cmdWriting = new NpgsqlCommand("INSERT INTO public.some_table (my_composite) VALUES (@composite)", conn);

var inventoryItem = new InventoryItem
{
  Name = "InventoryItemName",
  SupplierId = 4323,
  Price = 300.0m,
  ShortList = new short[] { 1, 2, 3 } 
};

cmdWriting.Parameters.AddWithValue("@composite", inventoryItem);
await cmdWriting.ExecuteNonQueryAsync(); 

The issue

The code above produces the following errors:

  • System.InvalidCastException: 'Writing values of 'npg_composite_mapping_object_issue.InventoryItem' is not supported for parameters having no NpgsqlDbType or DataTypeName. Try setting one of these values to the expected database type..'

  • InvalidOperationException: Cannot find property or field for composite field 'shortlist'.

The issue is the lack of a default type handler within Npgsql to map a C# short[] property to a PostgreSQL smallint[] field in a composite type.

Further technical details

  • Npgsql version: 8.0.2
  • Operating system: Windows and macOS

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions