Error on cascade deletion OneToOneField link with django-pyodbc backend

181 views
Skip to first unread message

Eugeny Klementev

unread,
Feb 18, 2012, 2:38:50 PM2/18/12
to django...@googlegroups.com
I have two models with OneToOne relation

class Unit (models.Model):
    label = models.CharField(max_length=32)
    datastream = models.OneToOneField ('Datastream', null=True, blank=True, related_name="unit")
    ...

class Datastream (models.Model):
    idd = models.AutoField(primary_key=True, auto_created=True, null=True)
    id = models.CharField(max_length=32)
    ...

During deletion of datastream these commands executed on DB server:

SQL: UPDATE "feeds_unit" SET "datastream_id" = NULL WHERE "id" IN (?, ?, ?)
PARAMS: (4, 5, 6)

SQL: DELETE FROM "feeds_unit" WHERE "id" IN (?, ?, ?)
PARAMS: (6, 5, 4)

I tried sqlite3 as backend - works fine.
But MSSQL raise error on "UPDATE "feeds_unit" SET "datastream_id" = NULL WHERE "id" IN (?, ?, ?)"
Error:
IntegrityError: ('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of UNIQU
E KEY constraint 'UQ__feeds_un__EAC1B8893E847883'. Cannot insert duplicate key in object 'dbo.feeds_
unit'. The duplicate key value is (<NULL>). (2627) (SQLExecDirectW); [01000] [Microsoft][ODBC SQL Se
rver Driver][SQL Server]The statement has been terminated. (3621)")

Can anybody help me?

akaariai

unread,
Feb 18, 2012, 5:12:13 PM2/18/12
to Django users
From the looks of it, it seems MSSQL isn't following the standard
regarding null equality in unique indexes. You might be interested in:
http://connect.microsoft.com/SQLServer/feedback/details/299229/change-unique-constraint-to-allow-multiple-null-values

Depending on your server version you might have a possibility to
define a filtered unique contraint. Otherwise, remove the constraint
in the database schema (the django model could still have the
OneToOneField for validation purposes) or alter your business logic in
a way that the referenced Unit models are also deleted.

- Anssi

On Feb 18, 9:38 pm, Eugeny Klementev <eugeny.klemen...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages