n8n/packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts
Elias Meire bed04ec122
fix(Postgres Node): Stop marking autogenerated columns as required (#8230)
## Summary

Postgres columns can be
- [generated as
identity](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-identity-column/)
- [generated by a custom
expression](https://www.postgresql.org/docs/current/ddl-generated-columns.html)

In these 2 cases, the column is not required when inserting a new row.
This PR makes sure these types of column are not marked required in n8n.

### How to test

1. Create a Postgres table with all types of generated columns:
for version >= 10
  ```sql
  CREATE TABLE "public"."test_table" (
      "id" int8 NOT NULL DEFAULT nextval('test_table_id_seq'::regclass),
      "identity_id" bigint GENERATED ALWAYS AS IDENTITY,
      "id_plus" numeric GENERATED ALWAYS AS (id + 5) STORED,
      "title" varchar NOT NULL,
      "created_at" timestamp DEFAULT now(),
      PRIMARY KEY ("id")
  )
  ```
  Before 10 you have to use serial or bigserial types:
  ```sql
  CREATE TABLE distributors (
     did    serial not null primary key,
     name   varchar(40) NOT NULL CHECK (name <> '')
);
  ```
2. Add a postgres node to canvas and try to insert data without the
generated columns
3. Should successfully insert

More info in Linear/Github issue ⬇️ 

## Related tickets and issues
- fixes #7084
-
https://linear.app/n8n/issue/NODE-816/rmc-not-all-id-fields-should-be-required
-
https://linear.app/n8n/issue/NODE-681/postgres-cant-map-automatically-if-database-requires-a-field

## Review / Merge checklist
- [ ] PR title and summary are descriptive. **Remember, the title
automatically goes into the changelog. Use `(no-changelog)` otherwise.**
([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md))
- [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up
ticket created.
- [ ] Tests included.
> A bug is not considered fixed, unless a test is added to prevent it
from happening again.
   > A feature is not complete without tests.

---------

Co-authored-by: Michael Kret <michael.k@radency.com>
2024-01-05 12:37:33 +01:00

38 lines
1.3 KiB
TypeScript

import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
import type pgPromise from 'pg-promise';
import type pg from 'pg-promise/typescript/pg-subset';
import type { Client } from 'ssh2';
export type QueryMode = 'single' | 'transaction' | 'independently';
export type QueryValue = string | number | IDataObject | string[];
export type QueryValues = QueryValue[];
export type QueryWithValues = { query: string; values?: QueryValues };
export type WhereClause = { column: string; condition: string; value: string | number };
export type SortRule = { column: string; direction: string };
export type ColumnInfo = {
column_name: string;
data_type: string;
is_nullable: string;
udt_name?: string;
column_default?: string;
is_generated?: 'ALWAYS' | 'NEVER';
identity_generation?: 'ALWAYS' | 'NEVER';
};
export type EnumInfo = {
typname: string;
enumlabel: string;
};
export type PgpClient = pgPromise.IMain<{}, pg.IClient>;
export type PgpDatabase = pgPromise.IDatabase<{}, pg.IClient>;
export type PgpConnectionParameters = pg.IConnectionParameters<pg.IClient>;
export type ConnectionsData = { db: PgpDatabase; pgp: PgpClient; sshClient?: Client };
export type QueriesRunner = (
queries: QueryWithValues[],
items: INodeExecutionData[],
options: IDataObject,
) => Promise<INodeExecutionData[]>;