Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration a model with a ManyToManyfield (MySQL) #1701

Open
dhayford92 opened this issue Aug 25, 2024 · 0 comments
Open

Migration a model with a ManyToManyfield (MySQL) #1701

dhayford92 opened this issue Aug 25, 2024 · 0 comments

Comments

@dhayford92
Copy link

dhayford92 commented Aug 25, 2024

#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'")"

@dhayford92 dhayford92 changed the title Migration a model with a ManyToManyfield Migration a model with a ManyToManyfield (MySQL) Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant