jesjos / jesjos/active_record_upsert

Upsert tries to insert NOT NULL columns as NULL

Open
#102 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
207
Forks
49
PR merge metrics
No merged PRs in 30d

Description

When upserting a record in a database column is not nullable and has a default value, ActiveRecord::Upsert attemps to insert a nil value. This raises an exception:

PG::NotNullViolation: ERROR: null value in column "uuid" violates not-null constraint

The table looks like this:

```sql
CREATE TABLE public.defaulting_records (
id bigint NOT NULL,
uuid uuid DEFAULT public.gen_random_uuid() NOT NULL,
name character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
```

And the SQL generated for the upsert is as follows:

```sql
INSERT INTO "defaulting_records" ("uuid", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) ON CONFLICT ("id") DO UPDATE SET "name" = $5, "updated_at" = $6 RETURNING *, (xmax = 0) AS _upsert_created_record
```

Failing test available in this branch: https://github.com/andrewclink/active_record_upsert/tree/upsert_null

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the failing test in the upsert_null branch and compare its generated SQL with the SQL shown in the issue. Confirm the upsert behavior for a NOT NULL column with a database default, then update the test or implementation as needed so the default is used instead of inserting NULL.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, ruby
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.