0
votes

I had the below Django model, with managed = False, now I needed to change it to managed = True and also add a char field of attribute choice Version 1

class TblHoldings(models.Model):
    item_code = models.CharField(unique=True, max_length=5)
    product_name = models.CharField(max_length=45)
    service_provider = models.ForeignKey(TblHoldingsServiceProviders,on_delete=models.CASCADE, related_name='service_provider',db_column='service_provider')
    account_details = models.CharField(max_length=100)
    purchase_cost = models.IntegerField()
    current_value = models.IntegerField()
    power_of = models.CharField(max_length=45, blank=True, null=True)
    purchase_date = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return self.product_name + ' at ' + self.service_provider.provider_name

    class Meta:
        verbose_name = 'Holding'
        verbose_name_plural = 'Holdings'
        managed = False
        db_table = 'tbl_holdings'

Version 2

FIN_GOAL_TERM =(
    ('L', 'Long Term 7+ years'),
    ('M', 'Medium Term 3-7 years'),
    ('S', 'Short Term <2 years'),
)
class TblHoldings(models.Model):
    item_code = models.CharField(unique=True, max_length=5)
    product_name = models.CharField(max_length=45)
    service_provider = models.ForeignKey(TblHoldingsServiceProviders,on_delete=models.CASCADE, related_name='service_provider',db_column='service_provider')
    account_details = models.CharField(max_length=100)
    purchase_cost = models.IntegerField()
    current_value = models.IntegerField()
    power_of = models.CharField(max_length=45, blank=True, null=True)
    purchase_date = models.DateTimeField(blank=True, null=True)
    goal_term = models.CharField(max_length=40, choices=FIN_GOAL_TERM)
    updated_at = models.DateTimeField(blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return self.product_name + ' at ' + self.service_provider.provider_name

    class Meta:
        verbose_name = 'Holding'
        verbose_name_plural = 'Holdings'
        managed = True
        db_table = 'tbl_holdings'

Now I get the below error on running make migrations, can anyone suggest how to solve this ??

File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 447, in add_field self.execute(sql, params) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute cursor.execute(sql, params) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute return super().execute(sql, params) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers return executor(sql, params, many, context) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute return self.cursor.execute(query, args) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 209, in execute res = self._query(query) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 315, in _query db.query(q) File "/root/appstaq_fin/venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 231, in query _mysql.connection.query(self, query) django.db.utils.OperationalError: (1060, "Duplicate column name 'power_of'")

1
You don't have a priority field in that table. Seems like you have it included in a ModelAdmin by accident?voodoo-burger
sorry priority field is actually goal_term let me update the errorichthyocentaurs
@ichtyocentaurs it looks like the migration is trying to create a column that already exists. Check the migration file and see what statements it's trying to executevoodoo-burger
You have an existing db table which you created a non-managed model from. Now you're telling Django to start managing it. So you need a fake initial migration that will tell Django about the existing columns, before you start adding new ones.Daniel Roseman
thanks that worked @DanielRosemanichthyocentaurs

1 Answers

0
votes

So I ran the below command on the suggestion from Daniel Roseman and it fixed my problem

python3 manage.py migrate --fake