Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

Django migration error when database has custom views

Django 3.1.

app folder : stocks

project folder with settings inside: main

I have this model:

class Bar(models.Model):
    stock = models.CharField(max_length=10)
    timestamp = models.DateTimeField()
    date = models.DateField()
    time = models.TimeField()
    open = models.DecimalField(max_digits=6, decimal_places=2)
    high = models.DecimalField(max_digits=6, decimal_places=2)
    low = models.DecimalField(max_digits=6, decimal_places=2)
    close = models.DecimalField(max_digits=6, decimal_places=2)
    is_regular = models.BooleanField()
    change = models.DecimalField(max_digits=10, decimal_places=4)

In the SQLite database I have created view daily_bottoms from that model using SQliteStudio.

Now I am trying to migrate because added another field. makemigrations works fine, but when trying to migrate I get an error:

django.db.utils.OperationalError: error in view daily_bottoms: no such table: main.stocks_bar

If I delete the view then it will migrate , but I would like to avoid that every time.

How to make it work?

question from:https://stackoverflow.com/questions/65645758/django-migration-error-when-database-has-custom-views

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your table is not managed by Django you should set

 class Meta:
       managed = False

on your model as documented

If False, no database table creation, modification, or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...