Home > OS >  django.db.utils.IntegrityError: null value in column "genre_id" of relation "shop_boo
django.db.utils.IntegrityError: null value in column "genre_id" of relation "shop_boo

Time:10-21

I am building Microservice, I have 2 project / 1 DB in Warehouse and 1 DB in Shop. When I have a new book in Warehouse I have updated DB in Shop - celery takes this task and does

Models in Warehouse and Shop it same.

I'm getting a strange error that I can't find information for.

Models:

from django.db import models
from django.urls import reverse


class Author(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __str__(self):
        return f'{self.first_name} {self.last_name}'


class Genre(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    slug = models.SlugField(max_length=50, unique=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'genre'
        verbose_name_plural = 'genres'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('shop:book_list_by_genre', args=[self.slug])


class Book(models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE)
    genre = models.ForeignKey(Genre,
                              related_name='books',
                              on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    language = models.CharField("language", max_length=20)
    pages = models.IntegerField()
    image = models.ImageField(upload_to='products/%Y/%m/%d')
    slug = models.SlugField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    isbn = models.CharField('ISBN', max_length=13,
                            unique=True)
    created = models.DateTimeField(auto_now_add=True)
    available = models.BooleanField(default=True)
    quantity = models.IntegerField()

    class Meta:
        ordering = ('title',)
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('shop:book_detail',
                       args=[self.id, self.slug])

Celery:

from celery import shared_task

from django.core.mail import send_mail

import requests

from .models import Author, Book, Genre


@shared_task
def send_mail_task(subject, message, email):
    send_mail(subject, message, email, ['[email protected]'])


@shared_task
def shop_sync():
    print('Starting update from warehouse api for database')
    print('Getting data from api...')

    url = 'http://warehouse:8001/warehouse/authors/'
    print('Clearing data...')

    response_author = requests.get(url)
    if response_author.status_code != 200:
        return
    response_data_author = response_author.json()
    while 1:
        for counter, data in enumerate(response_data_author['results']):
            Author.objects.get_or_create(
                id=data['id'],
                defaults={
                    'id': data['id'],
                    'first_name': data['first_name'],
                    'last_name': data['last_name']
                }
            )

        if response_data_author['next']:
            response_data_author = requests.get(response_data_author['next']).json()
        else:
            break

    url = 'http://warehouse:8001/warehouse/genres/'
    print('Clearing data...')

    response_genre = requests.get(url)
    if response_genre.status_code != 200:
        return
    response_data_genre = response_genre.json()

    while 1:
        for counter, data in enumerate(response_data_genre['results']):
            Genre.objects.get_or_create(
                id=data['id'],

                defaults={
                    'slug': data['slug'],
                    'name': data['name'],
                }
            )

        if response_data_genre['next']:
            response_data_genre = requests.get(
                response_data_genre['next']
            ).json()
        else:
            break

    url = 'http://warehouse:8001/warehouse/books/'
    print('Clearing data...')

    response_book = requests.get(url)
    if response_book.status_code != 200:
        return
    response_data_book = response_book.json()
    while 1:
        for counter, data in enumerate(response_data_book['results']):
            book, created = Book.objects.get_or_create(
                id=data['id'],
                defaults={
                    'id': data['id'],
                    "title": data['title'],
                    "description": data['description'],
                    "language": data['language'],
                    "pages": data['pages'],
                    "image": data['image'],
                    'slug': data['slug'],
                    "price": data['price'],
                    "isbn": data['isbn'],
                    "created": data['created'],
                    "available": data['available'],
                    "quantity": data['quantity'],
                    "author": Author.objects.get(id=data['author'])
                }
            )

            if not created:
                book.title = data['title']
                book.description = data['description']
                book.language = data['language']
                book.pages = data['pages']
                book.image = data['image']
                book.slug = data['slug']
                book.price = data['price']
                book.isbn = data['isbn']
                book.created = data['created']
                book.available = data['available']
                book.quantity = data['quantity']
                book.author = Author.objects.get(id=data['author'])
                book.save()

            for i in data['genre']:
                genre = Genre.objects.get(id=i)
                book.genre.add(genre)

        if response_data_book['next']:
            response_data_book = requests.get(response_data_book['next']).json()
        else:
            break
        print('Database was updated from warehouse api')

TRACEBACK:

-

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line
450, in trace_task R = retval = fun(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line
731, in protected_call return self.run(*args, **kwargs) File "/code/shop/tasks.py", line 78, in shop_sync book, created = Book.objects.get_or_create( File "/usr/local/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py",
line 588, in get_or_create return self.create(**params), True File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py",
line 453, in create obj.save(force_insert=True, using=self.db) File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py",
line 726, in save self.save_base(using=using, force_insert=force_insert, File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py",
line 763, in save_base updated = self._save_table( File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py",
line 868, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py",
line 906, in _do_insert return manager._insert( File "/usr/local/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py",
line 1270, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql cursor.execute(sql, params) File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute with self.db.wrap_database_errors: File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line
90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) django.db.utils.IntegrityError: null value in column "genre_id" of relation "shop_book" violates not-null constraint DETAIL: Failing row contains (7, INFERNO, Finding himself in the most mysterious Italian town - Florence, ..., English, 472,
http://warehouse:8001/media/products/2021/09/15/cover-50.jpg,
inferno, 445.00, 9780307736925, 2021-10-19 23:38:08.910299 00, t, 8,
1, null).

CodePudding user response:

your Book model has mandatory One-to-many relation to Genre

class Book(models.Model):
genre = models.ForeignKey(Genre,
                          related_name='books',
                          on_delete=models.CASCADE)

That means you have to provide value for genre in defaults if creating new Book

book, created = Book.objects.get_or_create(
            id=data['id'],
            defaults={
                'id': data['id'],
                'genre': # value needs to be provided here

There is a code later trying to add genres

for i in data['genre']:
    genre = Genre.objects.get(id=i)
    book.genre.add(genre)

But that code is to late and it expects Book to Genre being Many-to-many relation when if fact Book to Genre defined as One-to-Many relation

Something will have to change either data model or synchronisation script.

  • Related