Closed szinczen closed 1 year ago
@szinczen Thank you for posting an Issue
Are you able to provide me more details? Code snippet, Django/python version. I have problems with reproducing that
Django ~= 3.2.0
Django==3.2.13
django-db-views==0.1.4
mssql-django ~= 1.1.0
mssql-django==1.1.3
Python 3.8.10
msodbcsql18
@szinczen thank you, Could you provide me code snipped as well? Like your model definition before and after change, and deleted model
My models befor:
from django.conf import settings
from django.db import models
from django.utils import timezone
from django_db_views.db_view import DBView
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
class Meta:
managed = True
db_table = "blog_post"
class Client(DBView):
title = models.CharField(max_length=200)
view_definition = """
select top 1 title from blog_post
"""
class Meta:
managed = False
db_table = "[dbo].[Client]"
my models after delete class Client(DBView):
from django.conf import settings
from django.db import models
from django.utils import timezone
from django_db_views.db_view import DBView
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
class Meta:
managed = True
db_table = "blog_post"
when i make new model Client and i make makeviewmigrations i get:
class Migration(migrations.Migration):
dependencies = [
('blog', '0001_initial'),
]
operations = [
django_db_views.operations.ViewRunPython(
code=django_db_views.migration_functions.ForwardViewMigration('select top 1 title from blog_post', '[dbo].[Client]', engine='mssql'),
reverse_code=django_db_views.migration_functions.BackwardViewMigration('', '[dbo].[Client]', engine='mssql'),
atomic=False,
),
]
then i do makemigrations and get
# Generated by Django 3.2.13 on 2023-01-25 15:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0002_auto_20230125_1551'),
]
operations = [
migrations.CreateModel(
name='Client',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
],
options={
'db_table': '[dbo].[Client]',
'managed': False,
},
),
migrations.AlterModelOptions(
name='post',
options={'managed': True},
),
migrations.AlterModelTable(
name='post',
table='blog_post',
),
]
then after delete model i make makeviewmigrations and i get:
# Generated by Django 3.2.13 on 2023-01-25 15:51
from django.db import migrations
import django_db_views.migration_functions
import django_db_views.operations
class Migration(migrations.Migration):
dependencies = [
('blog', '0003_auto_20230125_1551'),
]
operations = [
django_db_views.operations.ViewDropRunPython(
code=django_db_views.migration_functions.DropView('[dbo].[Client]', engine='mssql'),
reverse_code=django_db_views.migration_functions.BackwardViewMigration('select top 1 title from blog_post', '[dbo].[Client]', engine='mssql'),
atomic=False,
),
]
after that operation makeviewmigrations stop working my requirments:
Django ~= 3.2.0
mssql-django ~= 1.0.0
django-jquery ~= 3.1.0
django-import-export ~= 2.8.0
pytest-django ~= 4.5.0
ntlm-auth ~= 1.5.0
requests ~= 2.28.0
requests-ntlm ~= 1.1.0
whitenoise ~= 6.2.0
exrex == 0.10.5
ldap3 ~= 2.9.0
argon2-cffi ~= 21.3.0
docutils ~= 0.18.1
django-db-views ~= 0.1.4
retry == 0.9.2
openpyxl ~= 3.0.10
factory-boy ~= 3.2.1
django-utils-six == 2.0
logging_tree ~= 1.9
gunicorn ~= 20.1.0 # WSGI
did you manage to generate issue?
@szinczen Yes, I recreated issue thank you for your effort, I will try to solve it as soon as I can
@szinczen I found an issue, The function that calculates unique table identifiers, didn't lowercase table names as Django does by default, that why there were a Key Error. You didn't see that Key error because there was a bug in the view context manager that hidden that.
When you download version 0.1.5 (I uploaded it few minutes ago, you can download it from PyPI) the makeviewmigrations command will start work
Unfortunately there is a third issue, If you add a new View with exactly same name as the deleted one, it will be skipped from creation. To fix that, I will have to put some more effort. I will try to fix that in 1.6, but I think that in 1.5 you will be able to use library without problems, so there is no rush I hope
Please let me know whether new version works for you :)
Thank again for reporting that issue
I'm have problem deleting views. When deleting a model, the migration file is generated correctly (includes django_db_views.operations.ViewDropRunPython)). However, with subsequent changes, you cannot generate another migration file with makeviewmigrations. The python manage.py makeviewmigrations command works but returns nothing, not even a message that no changes were detected in the database. If such changes also occur, the migration file will not be generated. I am using mssql. If I remove the ViewDropRunPython operations from the migration file, makeviewmigration starts working.