I havent defined more than one primary key. If primary key is defined, the automatic field wouldnt be generated, right?
Sincerely yours,
Joel G Mathew
On Sat, 22 Dec 2018 at 16:13, Glen D souza <glenpop54@gmail.com> wrote:
if you check the error messageDETAIL: Failing row contains (3, 1, Consultation OPD, 135, 135, null, 1).There are total of 7 valuesCheck your modelclass CGHSRates(models.Model):rid = models.AutoField(primary_key=True)serial = models.IntegerField(default=0)name = models.CharField(max_length=100)NonNrate = models.FloatField(blank=True)NABHrate = models.FloatField(blank=True)section = models.ForeignKey('CGHSSection', on_delete=models.SET_NULL, null=True, blank=True)There are total of 6 fieldsThere can be only one Primary KeySomewhere due to rid field its going wrong--On Sat, 22 Dec 2018 at 14:48, Joel Mathew <joel@joel.su> wrote:--Django - constraint errorI have the following python code in django which is deployed on heroku:def newimportdb(request):import xlrdloc = ("./CGHS Rates 2014- Trivandrum.xlsx")wb = xlrd.open_workbook(loc)sheet = wb.sheet_by_index(0)for i in range(sheet.nrows):try:sl = int(sheet.cell_value(i, 0))except:title = sheet.cell_value(i, 1)hd = CGHSSection(title = title)try:hd.save()heading = CGHSSection.objects.get(title = title)print(f"Saved new heading: {title}")continueexcept:print(f"Could not save new section: {title}. Maybe it existing. Reading it.")heading = CGHSSection.objects.get(title = title)continuetest = sheet.cell_value(i, 1)nonnabh = int(sheet.cell_value(i, 2))nabh = int(sheet.cell_value(i, 3))print(f'{i} Test:{test} NonNABH:{nonnabh} NABH:{nabh} Under:{heading}')it = CGHSRates(serial = sl, name =test, NABHrate = nabh, NonNrate=nonnabh, section=heading)print(f"Value of section heading is {heading.title}")print(f'it = CGHSRates(serial = {sl}, name ={test}, NABHrate = {nabh}, NonNrate={nonnabh}, section={heading}')it.save()print("Saved")In my models, I have:from django.db import modelsclass CGHSRates(models.Model):rid = models.AutoField(primary_key=True)serial = models.IntegerField(default=0)name = models.CharField(max_length=100)NonNrate = models.FloatField(blank=True)NABHrate = models.FloatField(blank=True)section = models.ForeignKey('CGHSSection', on_delete=models.SET_NULL, null=True, blank=True)class Meta:unique_together = ('name', 'NonNrate', 'NABHrate')class CGHSSection(models.Model):num = models.AutoField(primary_key=True)title = models.CharField(max_length=150, unique=True)On running the above, I get:[22/Dec/2018 09:07:55] "GET /favicon.ico/ HTTP/1.1" 404 3346Could not save new section: UNCLASSIFIED. Maybe it existing. Reading it.1 Test:Consultation OPD NonNABH:135 NABH:135 Under:CGHSSection object (1)Value of section heading is UNCLASSIFIEDit = CGHSRates(serial = 1, name =Consultation OPD, NABHrate = 135, NonNrate=135, section=CGHSSection object (1)Internal Server Error: /app/importTraceback (most recent call last):File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _executereturn self.cursor.execute(sql, params)psycopg2.IntegrityError: null value in column "section" violates not-null constraintDETAIL: Failing row contains (3, 1, Consultation OPD, 135, 135, null, 1).The above exception was the direct cause of the following exception:Traceback (most recent call last):File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in innerresponse = get_response(request)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_responseresponse = self.process_exception_by_middleware(e, request)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_responseresponse = wrapped_callback(request, *callback_args, **callback_kwargs)File "/home/joel/peroorkada/app/views.py", line 134, in newimportdbit.save()File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/base.py", line 718, in saveforce_update=force_update, update_fields=update_fields)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/base.py", line 748, in save_baseupdated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/base.py", line 831, in _save_tableresult = self._do_insert(cls._base_manager, using, fields, update_pk, raw)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/base.py", line 869, in _do_insertusing=using, raw=raw)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_methodreturn getattr(self.get_queryset(), name)(*args, **kwargs)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1136, in _insertreturn query.get_compiler(using=using).execute_sql(return_id)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sqlcursor.execute(sql, params)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in executereturn super().execute(sql, params)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in executereturn self._execute_with_wrappers(sql, params, many=False, executor=self._execute)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappersreturn executor(sql, params, many, context)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _executereturn self.cursor.execute(sql, params)File "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__raise dj_exc_value.with_traceback(traceback) from exc_valueFile "/home/joel/sampledjango/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _executereturn self.cursor.execute(sql, params)django.db.utils.IntegrityError: null value in column "section" violates not-null constraintDETAIL: Failing row contains (3, 1, Consultation OPD, 135, 135, null, 1).I cant understand why this is happening. My print just before saving data shows that section is not null. However that seems to be the error I'm getting
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_8qa0H%3DBgzgnX_cP63CZaYfzXgnS8VdoMH9ZBLwF1w-YQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHfehoX8POqSGSdEfN%2BrL01rQyiiBeYT50%3D%2BkgHoto0UzpW4DA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw__n%2Bi5srWCA23aVdbbKvkHn9nY3e_K0VutYegLbjVVxug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment