Home > database >  Errors in Migrating models
Errors in Migrating models

Time:10-11

Having errors in trying to migrate models(makemigrations command). Errors in classes Cart and Product. Here's the code:

class Cart(models.Model):
    type_status = (('Pending'), ('Ongoing'), ('Delivered'))
    type_payment = (('Yes'), ('No'))
    cart_id = models.AutoField(primary_key=True)
    status = models.CharField(max_length=1, choices=type_status)
    payment_paid = models.CharField(max_length=1, choices=type_payment)
    totalAmount = models.FloatField()

class Admin(models.Model):
    item_id = models.AutoField(primary_key=True)
    itemName = models.CharField(max_length=50, null=False)

class Product(models.Model):
    item_id = models.ForeignKey(Admin, on_delete=models.CASCADE)
    itemName = models.ForeignKey(Admin, on_delete=models.CASCADE)
    price = models.FloatField()

Errors: registration.Cart.payment_paid: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples. registration.Cart.status: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples. registration.Product.itemName: (fields.E304) Reverse accessor 'Admin.product_set' for 'registration.Product.itemName' clashes with reverse accessor for 'registration.Product.item_id'. HINT: Add or change a related_name argument to the definition for 'registration.Product.itemName' or 'registration.Product.item_id'. registration.Product.item_id: (fields.E304) Reverse accessor 'Admin.product_set' for 'registration.Product.item_id' clashes with reverse accessor for 'registration.Product.itemName'.
HINT: Add or change a related_name argument to the definition for 'registration.Product.item_id' or 'registration.Product.itemName'
.

CodePudding user response:

The Reverse Accessor error is caused by similar field names in Admin and Product models.

Change field names of Product model, or add related_name attribute and define a different name. (E.g. p_item_name, p_item_id)

sample code:

class Product(models.Model):
    item_id = models.ForeignKey(Admin, related_name="p_item_id", 
                                on_delete=models.CASCADE)
    itemName = models.ForeignKey(Admin, related_name="p_item_name", 
                                 on_delete=models.CASCADE)
    price = models.FloatField()

CodePudding user response:

You need to pass tuples in your choices. You also need to set max_length at least as longest value in choices.

type_status = (('pending', 'Pending'), ('ongoing', 'Ongoing'), ('delivered', 'Delivered'))
type_payment = (('yes', 'Yes'), ('no', 'No'))

First string is a value that you keep in DB, second one is human-readable (that you will see in select input).

See DOCS

CodePudding user response:

This is not the correct way that you did for choices:

Do like below code:

type_status = (
           ('Pending', 'Pending'), 
           ('Ongoing', 'Ongoing'), 
           ('Delivered', 'Delivered'),
)

This error will solve And don't forget to give max_length=100 in that model field where you added CHOICES

CodePudding user response:

Give ForeignKey.related_name for both the foreign keys i.e. item_id and itemName whatever you want so that it will not clash:

class Product(models.Model):
    item_id = models.ForeignKey(Admin, on_delete=models.CASCADE, related_name="id_item")
    itemName = models.ForeignKey(Admin, on_delete=models.CASCADE, related_name="name_of_item")
    price = models.FloatField()

Then kindly run migrate commands.

Note: Generally, fields of the models are writing in pascal_case so you may change it to total_amount from totalAmount in Cart model. Also it is not good practice to name same foreign keys in both the models.

  • Related