michiya / django-pyodbc-azure

Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc
https://pypi.python.org/pypi/django-pyodbc-azure
BSD 3-Clause "New" or "Revised" License
321 stars 140 forks source link

Violation of Unique Key constraint #14

Open ninajlu opened 10 years ago

ninajlu commented 10 years ago

('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of UNIQUE KEY constraint 'UQaccounts70648D1C466017C6'. Cannot insert duplicate key in object 'dbo.accounts_telemetryevent'. The duplicate key value is (). (2627) (SQLExecDirectW); [01000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)") I am using pyodbc as follows to link a Django app to Azure SQL db: DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc', 'NAME': 'db', 'USER': 'xxxx', 'PASSWORD': 'xxxx', 'HOST': 'xxx', 'PORT': '1433', 'OPTIONS': { 'use_legacy_datetime' : True, },

   }
}

I added use_legacy_datetime to correct for an error that said Unicode object didn't have timezones. However, it does not seem like the database is creating primary keys correctly since it is trying to add a new entry with a key that is NULL when it usually just generates a unique primary key in another DB like MySQL or SQLite, which I was using before. Do you have any thoughts on why this might be? The unique primary key of a new entry should not be NULL--it should be generated automatically and be unique and it isn't doing that for some reason. Thus, any additions to the db just aren't working due to duplicate unique keys that are Null.

michiya commented 10 years ago

Thank you for reporting and sorry for my delay. Could you give me your model definition and code snippet to reproduce the error?

hoscanoa commented 8 years ago

image

class Menu(models.Model):
    descripcion = models.CharField(verbose_name='descripción', max_length=20)
    link = models.CharField(max_length=100)
    icono = models.CharField(verbose_name='ícono', max_length=100, blank=True, null=True)
    padre = models.OneToOneField('self', blank=True, null=True)