关系不存在:原始sql查询django ---我有正确的表名:app name _ model name:它必须是别的东西

时间:2015-12-05 19:40:29

标签: django postgresql django-models psycopg2

您好我在Django中运行原始SQL查询,

我一直在关系" makeprofile_compositemodel"不存在

模型名称是compositeModel,应用程序是makeprofile,当在postgres中查看db时,表的名称是正确的并且复制&如此粘贴。我只是不知道我做错了什么或者为什么会出现这个错误....如果有人有任何建议请告诉我。

con =None

def LocData (LocList):

    if len(LocList) == 1:

        try:

            cursor = connection.cursor()
            cursor.execute("""SELECT US.*

                    FROM        makeProfile_compositemodel US, makeProfile_profile UP

                    WHERE       US.profile_id = UP.user_id AND
                                UP.country = %s




                        """,[LocList[0]])


            while True:


                rows = cursor.fetchmany(1000000)

                if not rows:
                      break
                print("+++++++++",len(rows))

                DictReturn.update(TheOtherGuy(rows))


        except psycopg2.DatabaseError as e:

            print ('Error' ,e)
            sys.exit(1)


        finally:

            if cursor:
                cursor.close()

    else:
        return None

错误消息

Exception Type: ProgrammingError
Exception Value:    
relation "makeprofile_compositemodel" does not exist
LINE 3:                     FROM        makeProfile_compositemodel U...
                                        ^
Exception Location: /home/g4/django/ruPure/lib/python3.4/site-packages/django/db/backends/

  utils.py in execute, line 64

TRACEBACK

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/linkMatchs/

Django Version: 1.8.7
Python Version: 3.4.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.admin',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'registration',
 'newsletter',
 'questions',
 'makeProfile',
 'linkMatchs',
 'home')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/g4/django/ruPure/src/linkMatchs/views.py" in index
  43.         query_set = lookUp(a=None, **kwargs)
File "/home/g4/django/ruPure/src/linkMatchs/search.py" in lookUp
  249.     LocData(LocList)
File "/home/g4/django/ruPure/src/linkMatchs/search.py" in LocData
  137.                         """,[LocList[0]])
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/db/utils.py" in __exit__
  98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)
File "/home/g4/django/ruPure/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /linkMatchs/
Exception Value: relation "makeprofile_compositemodel" does not exist
LINE 3:                     FROM        makeProfile_compositemodel U...
                                        ^

谢谢

2 个答案:

答案 0 :(得分:0)

确定如此奇怪,但因为应用程序名为:makeProfile,因此表名为:makeProfile_compositemodel它无法找到它....

所以:解决办法是让应用程序名称字母全部小写,这导致表makeprofile_compositemodel .....这解决了这个问题... 或者:您可以将makeProfile_compositemodel放在应该使用的“”中 PS不要忘记进行新的迁移,因为会有一个新的表....

答案 1 :(得分:0)

您可以简单地使用

>>>obj=Model.objects.raw("""SELECT "colName" FROM table where "colname"= 'value(if it is a string)' """)

>>> for o in obj:

... print(o)