tortoise/tortoise-orm

Migration a model with a ManyToManyfield

dhayford92 opened this issue · 0 comments

#When i run a migration and create all tables for the models below.
I run into the error when creating invoice and trying to add invoiceitem to that invoice
Error:"(1146, "Table 'defaultdb.invoices_invoice_items' doesn't exist")"

from enum import Enum
from tortoise.models import Model
from tortoise import fields

# --- Invoice Item Model ---
class InvoiceItemModel(Model):
id = fields.UUIDField(pk=True)
item = fields.ForeignKeyField("models.ItemModel", related_name="invoice_items", on_delete=fields.CASCADE)
totalAmount = fields.DecimalField(max_digits=10, decimal_places=2)
quantity = fields.IntField()
numInstalment = fields.IntField()
paymentPlanPrice = fields.DecimalField(max_digits=10, decimal_places=2)
description = fields.TextField(null=True)
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)

def __str__(self):
    return self.item

class Meta:
    table = "invoice_items"
    table_description = "Invoice Items Model"
    ordering = ["-created_at"]

**# --- Invoice Status Model --- **
class InvoiceStatus(Enum):
New = "New"
Active = "Active"
Overdue = "Overdue"
Completed = "Completed"
Cancelled = "Cancelled"
Refund = "Refund"

# --- Invoice Model ---
class InvoiceModel(Model):
id = fields.UUIDField(pk=True)
invoiceNumber = fields.CharField(max_length=255, unique=True)
customer = fields.ForeignKeyField("models.CustomerModel", related_name="invoices", on_delete=fields.CASCADE)
items = fields.ManyToManyField("models.InvoiceItemModel", related_name="invoices", through="invoices_invoice_items")
vat = fields.DecimalField(max_digits=10, decimal_places=2)
covidTax = fields.DecimalField(max_digits=10, decimal_places=2)
totalAmount = fields.DecimalField(max_digits=10, decimal_places=2)
subTotal = fields.DecimalField(max_digits=10, decimal_places=2)
status = fields.CharEnumField(InvoiceStatus, default=InvoiceStatus.New)
paymentType = fields.CharField(max_length=255)
paymentPlanAmount = fields.DecimalField(max_digits=10, decimal_places=2)
note = fields.TextField(null=True)
overDueDate = fields.DatetimeField(null=True)
setDate = fields.DatetimeField(null=True)
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)

def __str__(self):
    return self.invoiceNumber

class Meta:
    table = "invoices"
    table_description = "Invoice Model"
    ordering = ["-created_at"]

after manually creating "invoices_invoice_items" using the sql schema below

example:
"CREATE TABLE IF NOT EXISTS invoices_invoice_items (
invoice_id CHAR(36) NOT NULL,
invoice_item_id CHAR(36) NOT NULL,
CONSTRAINT fk_invoices_invoice_items_invoice_id FOREIGN KEY (invoice_id) REFERENCES invoices (id) ON DELETE CASCADE,
CONSTRAINT fk_invoices_invoice_items_invoice_item_id FOREIGN KEY (invoice_item_id) REFERENCES invoice_items (id) ON DELETE CASCADE,
PRIMARY KEY (invoice_id, invoice_item_id)
) CHARACTER SET utf8mb4 COMMENT='Join table for InvoiceModel and InvoiceItemModel';"

Now when i try to create an invoice after building all the tables. The new error i get is "(1054, "Unknown column 'invoices_id' in 'where clause'")"