'table app_shop_boughtitem has no column named buyer_id Django
Can you explain me, why I have this error? I think migration should solve this problem, but it doesn't. How can I solve this problem?
My model:
class BoughtItem(models.Model):
name = models.CharField(max_length=100, verbose_name='Название товара или акции', blank=True)
price = models.IntegerField(verbose_name='Цена', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
buyer = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Покупатель')
This migration:
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('app_shop', '0002_item'),
]
operations = [
migrations.CreateModel(
name='BoughtItem',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=100, verbose_name='Название товара или акции')),
('price', models.IntegerField(blank=True, verbose_name='Цена')),
('created_at', models.DateTimeField(auto_now_add=True)),
('buyer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Покупатель')),
],
),
]
Solution 1:[1]
Delete the latest files in the migrations folder of the app then makemigrations again and migrate. Also try to put related_name param in the ForeignKey field like: models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='??????????', related_name='bought_items')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aneeq Ak |
