Thanks to visit codestin.com
Credit goes to github.com

Skip to content

onConflict/merge don't work with Knex.CompositeTableType #4246

@Naddiseo

Description

@Naddiseo

Environment

Knex version: 0.21.15
Database + version: postgresql 13
OS: ubuntu

@lorefnon.

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do
    onConflict and merge should interact nicely with Knex.CompositeTableType

  2. Error message
    for .onConflict: Argument of type 'string' is not assignable to parameter of type 'readonly ("base" | "insert" | "update")[]

for .merge: Argument of type '{ insert_only_field: string; }' is not assignable to parameter of type 'Readonly<Partial<MaybeRawRecord<CompositeTableType<DBUser, Pick<DBUser, "name" | "insert_only_field">, Pick<DBUser, "name">>>>>'. Object literal may only specify known properties, and 'insert_only_field' does not exist in type 'Readonly<Partial<MaybeRawRecord<CompositeTableType<DBUser, Pick<DBUser, "name" | "insert_only_field">, Pick<DBUser, "name">>>>>'

  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real
    database connection to MySQL or PostgreSQL, then single file example which initializes
    needed data and demonstrates the problem.
import Knex from "knex";
import knex from './client';

declare module "knex/types/tables" {
  interface DBUser {
    id: string;
    insert_only_field: string;
    name: string;
  }
  interface Tables {
    users: Knex.CompositeTableType<DBUser, 
       // insert
       Pick<DBUser, 'name' | 'insert_only_field'>,
       // update
      Pick<DBUser, 'name'>
    >;
  }
}

await knex('users')
    .insert({ name: "Bob", insert_only_field: "1" })
    .onConflict("insert_only_field") // type error here
    .merge({
      name: "Bob",
      insert_only_field: knex.raw("EXCLUDED.insert_only_field"), // raw also fails with different error
    })
      ;   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions