Hello im learning Django and trying to add a new table where I save my isbn numbers and I wanted to make onetoone relation book with isbn number. Django creates the tables of isbn and books aswell. the problem when I do the onetoonefield.
models.py
from django.db import models
class BookNumber(models.Model):
isbn_10 = models.CharField(max_length=10, blank=True)
isbn_13 = models.CharField(max_length=13, blank=True)
class Book(models.Model):
title = models.CharField(max_length=36, blank=False, unique=True)
description = models.TextField(max_length=266, blank=True, unique=False)
price = models.DecimalField(default=0, max_digits=6, decimal_places=2)
published = models.DateField(blank=True, null=True)
is_published = models.BooleanField(default=False)
file = models.FileField(upload_to='covers/', blank=True)
cover = models.ImageField(upload_to='covers/', blank=True)
#numbers = models.OneToOneField(BookNumber, on_delete=models.CASCADE, null=True, blank=True)
def __str__(self):
return self.title
when I uncomment the numbers it throws the next error:
virtenv/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute
return super().execute(query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: **no such column: demo_book.number_id**
from the SQLLite:
sqlite> .tables
auth_group demo_book
auth_group_permissions demo_booknumber
auth_permission django_admin_log
auth_user django_content_type
auth_user_groups django_migrations
auth_user_user_permissions django_session
authtoken_token
sqlite> .schema demo_book
CREATE TABLE IF NOT EXISTS "demo_book" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(36) NOT NULL UNIQUE, "cover" varchar(100) NOT NULL, "description" text NOT NULL, "file" varchar(100) NOT NULL, "is_published" bool NOT NULL, "price" decimal NOT NULL, "published" date NULL);
sqlite> .schema demo_booknumber
CREATE TABLE IF NOT EXISTS "demo_booknumber" ("isbn_10" varchar(10) NOT NULL, "isbn_13" varchar(13) NOT NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT);
Can you please help?
I have erased the sqlite db and recreated the DB and I have added id fields or not it didn’t fix the error. Obviously I also asked chatgpt and follow the steps suggested aswell. It’s this one line that gives the problem. models.OneToOneField