Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30510 closed Bug (fixed)

bulk_create() crashes with mixed length arguments on LOB fields on Oracle.

Reported by: Mark Gordon Owned by: Ahmet Kucuk
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: oracle bulk_create
Cc: Georgi Yanchev Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the below model, containing one LOB field

from django.db import models

class Bar(models.Model):
    baz = models.TextField()

When you make a bulk_create request, e.g. Bar.objects.bulk_create([b0, b1]) you will get generate a query like below.

INSERT INTO "PROJECT_BAR" ("BAZ") SELECT * FROM (SELECT :arg1 col_0 FROM DUAL UNION ALL SELECT :arg0 FROM DUAL)

This works most of the time, however at some point logic was added to automatically convert a string's type to Database.CLOB when it exceeded 4000 bytes from a normal string literal type. When this conversion happens for some of the arguments but not all of the arguments in the ephemeral table Oracle will complain that the type of the column of the unified table is inconsistent and fail the query.

E.g. the following will fail

Bar.objects.bulk_create([Bar(baz='aaa'), Bar(baz='a'*5000)])

Generating the error

django.db.utils.DatabaseError: ORA-01790: expression must have same datatype as corresponding expression

Note that when both objects have a long or both have a short baz field this query succeeds.

I'm working on a patch

Change History (6)

comment:1 by Mark Gordon, 5 years ago

Owner: changed from nobody to Mark Gordon
Status: newassigned

comment:2 by Mariusz Felisiak, 5 years ago

Has patch: set
Keywords: oracle bulk_create added
Summary: Bulk creates with Oracle backend fail with mixed length arguments on LOB fieldbulk_create() crashes with mixed length arguments on LOB fields on Oracle.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 2.1master

Thanks for the report. Reproduced at 1d25354fb5f87d35968cd78b53d9560fd75f5b1a.

PR

comment:3 by Mariusz Felisiak, 5 years ago

Owner: changed from Mark Gordon to Ahmet Kucuk

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In dc890bef:

Fixed #30510 -- Fixed crash of QuerySet.bulk_create() with mixed-length texts on Oracle.

Text with more than 4000 characters must be set to as a CLOB on Oracle
what caused a mixed datatype error (ORA-01790) when shorter text
appeared in the same operation.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 9dc13f4:

[3.0.x] Fixed #30510 -- Fixed crash of QuerySet.bulk_create() with mixed-length texts on Oracle.

Text with more than 4000 characters must be set to as a CLOB on Oracle
what caused a mixed datatype error (ORA-01790) when shorter text
appeared in the same operation.

Backport of dc890bef5ad8e9fccce55f3e64af72103ea6e8c1 from master

comment:6 by Georgi Yanchev, 5 years ago

Cc: Georgi Yanchev added
Note: See TracTickets for help on using tickets.
Back to Top