1

I try to variabilise schema name because i use different environnment for my project. So i do this in my models.py:

    # from django.contrib.auth.models import AbstractUser 
from django.contrib.auth.models import AbstractUser
#Pour plus d'information sur le fonctionnement des models : https://docs.djangoproject.com/fr/5.1/topics/db/models/
from django.conf import settings

if settings.ENV == "DEV":
    schema="applications_db"
elif settings.ENV == "VIP":
    schema="applications_vip_db"
elif settings.ENV == "PROD":
    schema="applications_prod_db"

class ApplicationDjango(models.Model):
    a_name = models.CharField(max_length=100,verbose_name="Nom")
    a_portail_name = models.CharField(max_length=100,verbose_name="Nom portail")
    a_views_name = models.CharField(max_length=100,verbose_name="Views name")
    a_url_home = models.CharField(max_length=100,verbose_name="Url home")

    def __str__(self):
        return self.a_name+"_"+self.a_portail_name
    #class pour ajouter une contrainte d'unicité
    class Meta:
        managed= True
        db_table = f'{schema}.\"ApplicationDjango\"'

i make my migration --> noproblem

then when i migrate i got this error :

./manage.py migrate Applications
Operations to perform:
  Apply all migrations: Applications
Running migrations:
  Applying Applications.0004_alter_applicationdjango_table_alter_user_table...Traceback (most recent call last):
  File "/home/webadmin/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.SyntaxError: syntax error at or near "."
LINE 1: ...ons_applicationdjango" RENAME TO "applications_db"."Applicat...
                                                             ^

i try several things but it dont work. I hope that i can variablise schema name :/

Thanks for the help

4
  • What db are you using? Commented May 26 at 11:01
  • its a postgresql database Commented May 26 at 11:04
  • You should not edit the question with the new error logs. Your original error has been resolved. Please revert your edits. The new error is a different question which has been answered multiple times on Stack Overflow. I shared a link in the answer comments that can help you. Commented May 26 at 11:48
  • I have rolled back the edit. Please open a new question for your new errors if you wish. Commented May 26 at 11:59

1 Answer 1

2

Your error message suggests that the backslash in your database table name is causing the issue. You seem to be trying to escape the double quotes. So you should rename db_table = f'{schema}.\"ApplicationDjango\"'to:

db_table = f"{schema}.'ApplicationDjango'"

Get rid of the backslashes and use the double quotes to enclose the db_table value and see if that fixes your issue.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok i'll try it !
well i replace the db_table like this :db_table = f"{schema}.'ApplicationDjango'" and now i have this error : return self.cursor.execute(sql) psycopg2.errors.UndefinedTable: relation "Applications_applicationdjango" does not exist it look like the table name is in lower case :(
your original question has been resolved. This is another question but I'll be happy to help. send the complete error you get
Why i have this migrate error ?
Original problem has been resolved. You can accept the answer. Please see forum.djangoproject.com/t/… for help in resolving the new errors
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.