django-database-view icon indicating copy to clipboard operation
django-database-view copied to clipboard

Django Test Error - relation does not exist

Open vinoalterra opened this issue 4 years ago • 4 comments
trafficstars

Thanks for a great package. I have problem while running Django Test after creating DB View using Django Migration.

Test Case:

  • Python Version: 3.8.6 (Mac OSX)
  • Packages:
    • Django==3.1.1
    • django-database-view==0.2.1
  • Scenario
    • Create DB View in model (ref: https://pypi.org/project/django-database-view )
    • Running python manage.py migrate without any problem
    • Running python manage.py. test with this error

Screen Shot 2021-02-11 at 15 02 10

Any help?

vinoalterra avatar Feb 11 '21 08:02 vinoalterra

For current solution, I must set Model Meta with managed = False option

from dbview.models import DbView

class ProductCategoriesCounterView(DbView):

    class Meta:
        managed = False
        db_table = "product_categories_counter"
        
    @classmethod
    def get_view_str(cls):
        return """
            CREATE OR REPLACE VIEW product_categories_counter AS (SELECT
                pc.id,
                pc.name,
                pc.slug,
                (SELECT COUNT(1) FROM product_product_categories WHERE category_id=pc.id) AS total_product,
                pc.parent_id as parent_id,

                pc.created,
                pc.updated,
                pc.deleted,
                pc.image,
                pc.image_caption
            FROM product_categories pc
            LEFT JOIN product_categories pcx ON pcx.id=pc.parent_id
        )"""
        

vinoalterra avatar Feb 11 '21 08:02 vinoalterra

Looks like Meta.db_table is ignored for some reason. I'm not sure how to reproduce this though…

moorchegue avatar Sep 08 '21 16:09 moorchegue

Hi guys, i'm working with python 3.7.4, django 3.0.5, and django-database-view 0.3.0; with a Postgresql database. All works fine for me.

When you work with a DB view you must provide some field as a primary_key=True inside your django model code, for example: field_x = models.UUIDField(primary_key=True); but you must ensure that your DB view (sql statement) does have this specific field (field_x). There's no need to make a relation field like OneToOneField, ForeignKey, etc (unless your DB view require this, and only when the DB view meets the condition that it only has a unique register for that relation id).

By the other side, as @vinoalterra said, when you work with a DB view in Django, you must define the meta options AFTER YOU RUN MIGRATIONS (don't use this before you create your DB view) class Meta: managed = False db_table = "db_view_name"

Where your db_view_name must be the same in the sql statement: CREATE VIEW db_view_name AS.... IMPORTANT: I've only tested it using the get_view_str method (pure SQL statement)

jrcorralesf avatar Oct 01 '21 18:10 jrcorralesf

@vinoalterra

  • Create DB View in model (ref: https://pypi.org/project/django-database-view )
  • Running python manage.py migrate without any problem

Can you please show your migration file and the output of running migrate?

  • Running python manage.py. test with this error

Can you please re-run the test with verbosity increased (python manage.py test -v3), so we can confirm the migration is being applied?

tysonclugg avatar Jul 08 '22 07:07 tysonclugg