使用 Oracle 11g 数据库问题配置 django

Nag*_*una 2 python oracle django

使用 Django 配置 Oracle 数据库并在迁移应用程序时遇到错误

django.db.migrations.exceptions.MigrationSchemaMissing:无法创建 dja ngo_migrations 表(ORA-02000:缺少 ALWAYS 关键字)

application environment 
1.windows10
2.Python 3.6.x
3.Django 2.0.2
4.oracle 11g XE
in settins.py file 
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.oracle',
    'NAME': 'xe',
    'USER': 'abc',
    'PASSWORD':'xxxx',
    'HOST':'localhost',
    'PORT':"1521",

}
Run Code Online (Sandbox Code Playgroud)

小智 6

删除上面提到的代码中的 self.cursor.numbersAsStrings = True


小智 5

问题是 Django 2.0.2 只支持 oracle 12g。检查这个:

如何让 Django 2.0 使用 Oracle 11g 语法而不是 12c?

此外,您可以检查 sql 失败,如以下问题所示(在 manage.py 中添加一个 print(query) 行)

无法创建 django_migrations 表(ORA-02000:缺少 ALWAYS 关键字)

我已按照第一个问题中的建议降级到 Django 1.11,但这导致我出现错误“AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'”,因为我安装了最新的 cx_Oracle 版本。(更多信息在这里:https : //code.djangoproject.com/ticket/28138

为了解决这个问题,我将文件C:\Program Files\Python37\lib\site-packages\django\db\backends\oracle\base.py修改为:

def __init__(self, connection):
     self.cursor = connection.cursor()
     # Necessary to retrieve decimal values without rounding error.
     self.cursor.numbersAsStrings = True
     self.cursor.outputtypehandler = self._output_type_handler
     # Default arraysize of 1 is highly sub-optimal.
     self.cursor.arraysize = 100
     # https://github.com/django/django/commit/d52577b62b3138674807ac74251fab7faed48331

 @staticmethod
 def _output_type_handler(cursor, name, defaultType, length, precision, scale):
     """
     Called for each db column fetched from cursors. Return numbers as
     strings so that decimal values don't have rounding error.
     """
     if defaultType == Database.NUMBER:
         return cursor.var(
             Database.STRING,
             size=255,
             arraysize=cursor.arraysize,
             outconverter=str,
         )
Run Code Online (Sandbox Code Playgroud)

我从这里拿了这个代码块:

https://github.com/cloudera/hue/commit/07d85f46eeec9c8c19d9aa11d131638e2a99e65c#diff-6d9bd161753aad635c23c2e91efafe91

有了这个,至少我已经能够迁移这个项目了。我不知道它在进一步发展时是否会失败。

希望这可以帮助!

PD:我认为您的 DATABASES 设置应该与http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/python_django/python_django.htm 中的一样

DATABASES = {
'default': {
    'ENGINE':   'django.db.backends.oracle',
    'NAME':     'localhost/orcl',
    'USER':     'pythonhol',
    'PASSWORD': 'welcome',
}}
Run Code Online (Sandbox Code Playgroud)