Reputation: 13
my dev environment is in python 3.6 and virtualenv(virtualenv 15.1.0) and with dependencies below: django 2.2 ibm-db 3.0.1 ibm-db-django 1.2.0.0
as i used "(env_django2) λ python manage.py check", it return "System check identified no issues (0 silenced)." that is no problom.
however, when i used "python manage.py inspectdb --database db2 DEMO.DEMOUSER", it return like these below:
(env_django2) λ python manage.py inspectdb --database db2 DEMO.DEMOUSER
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
# Unable to inspect table 'DEMO.DEMOUSER'
# The error was: 'NoneType' object is not subscriptable
this is my db2 setting:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'db2':{
'ENGINE': 'ibm_db_django',
'NAME': 'test',
'USER': 'db2inst1',
'PASSWORD': 'XXXXXX',
'HOST': 'XX.XXX.XXX.XXX',
'PORT': 'XXXXX',
'PCONNECT':True,
}
}
and i had test it by coding like these below, and that is no problom.
with connections['db2'].cursor() as cursor:
cursor.execute("""
select * from DEMO.DEMOUSER
""")
columns = [col[0] for col in cursor.description]
listResult = [dict(zip(columns, row)) for row in cursor.fetchall()]
print(listResult)
so my question is how can i make the inspectdb work?
i have check inspectdb.py, and i found that
table_description = connection.introspection.get_table_description(cursor, table_name)
except Exception as e:
yield "# Unable to inspect table '%s'" % table_name
func get_table_description will invole the code below:
sql = "SELECT TYPE FROM SYSIBM.SYSTABLES WHERE CREATOR='%(schema)s' AND NAME='%(table)s'" % {'schema': schema.upper(), 'table': table_name.upper()}
it seem i didn't set the table and schema name right.
ok, i have to go to sleep, very tired.
2020.3.13 update........
so when i readed the code in ibm_db_django\introspection.py, i found that below:
schema = cursor.connection.get_current_schema()
that mean, ibm_db_django will get the schema by default, which is your user name setted in django's setting file. so it will always set the table name like 'db2inst1.DEMO.DEMOUSER' in my case. u can't change the schema by yourself.
as i know, setting the schema immutable is unreasonable,because the schema and user was different things in db2 unlike oracle. everyone can use the granted schema.
hence, i had to change the code like these,so that i can set my schema correctly:
# change the code below
#schema = cursor.connection.get_current_schema()
print( '-----------change by HK 20200313-----------------')
if len(table_name.split('.')) == 2:
cursor.connection.set_current_schema(table_name.split('.')[0].upper())
table_name = table_name.split('.')[1]
schema = cursor.connection.get_current_schema()
print('----------------- HK TEST schema:%s'%schema)
# change the code below
#cursor.execute( "SELECT * FROM %s FETCH FIRST 1 ROWS ONLY" % qn( table_name ) )
sql = "SELECT * FROM %s FETCH FIRST 1 ROWS ONLY" % ( schema+'.'+table_name )
print('------------ HK TEST LOG:%s'%sql)
cursor.execute( sql )
so the problem had been solved,however,another emerged !! when i typed the command "python manage.py inspectdb --database db2 DEMO.DEMOUSER", it show below:
class DemoDemouser(models.Model):
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\commands\inspectdb.py", line 34, in handle
for line in self.handle_inspection(options):
File "D:\MyProject\python\django-sample\env_django2\lib\site-packages\django\core\management\commands\inspectdb.py", line 102, in handle_inspection
column_name = row.name
AttributeError: 'list' object has no attribute 'name'
checking inspectdb.py,i find out the answer. the description of specified table ,returned from the func in introspection.py implemented by ibm_db_django, is different from inspectdb.py in django 2.2.
so check it with django release history and ibm_db_ango. ibm_db_ango 1.2 released in 2018.4.3, django 2.2 released in 2019.3.
it seem django 2.2 dont support the ibm_db_ango 1.2(the latest version)
therefore i had to install django 2.0 instead.
finally it work!!!.
Upvotes: 1
Views: 872