Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Test Error - relation does not exist #7

Open
vinoalterra opened this issue Feb 11, 2021 · 4 comments
Open

Django Test Error - relation does not exist #7

vinoalterra opened this issue Feb 11, 2021 · 4 comments

Comments

@vinoalterra
Copy link

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

Screen Shot 2021-02-11 at 15 02 10

Any help?

@vinoalterra
Copy link
Author

vinoalterra commented Feb 11, 2021

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
        )"""
        

@moorchegue
Copy link
Collaborator

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

@jrcorralesf
Copy link

jrcorralesf commented Oct 1, 2021

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)

@tysonclugg
Copy link

tysonclugg commented Jul 8, 2022

@vinoalterra

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants