2013-08-16 43 views
3

試圖用Django將數據插入到我的數據庫表中。這是我的表我想插入模式:Django插入Oracle數據庫無效標識符

class RunableFilters(models.Model): 
    equipment_id = models.BigIntegerField(null=True, blank=True) 
    filter_file_name = models.CharField(max_length=255, blank=True) 
    last_updated = models.CharField(max_length=255, blank=True) 
    class Meta: 
     db_table = 'runable_filters' 

錯誤,我得到:

>>> from books.models import RunableFilters 
>>> p1 = RunableFilters(equipment_id = '123456778', filter_file_name = "test_file_name", last_updated = "2013-16-8") 
>>> p1.save() 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "C:\Python27\lib\site-packages\django\db\models\base.py", line 546, in save 
    force_update=force_update, update_fields=update_fields) 
    File "C:\Python27\lib\site-packages\django\db\models\base.py", line 650, in save_base 
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw) 
    File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 215, in _insert 
    return insert_query(self.model, objs, fields, **kwargs) 
    File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1675, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 937, in execute_sql 
    cursor.execute(sql, params) 
    File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 41, in execute 
    return self.cursor.execute(sql, params) 
    File "C:\Python27\lib\site-packages\django\db\backends\oracle\base.py", line 717, in execute 
    six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2]) 
    File "C:\Python27\lib\site-packages\django\db\backends\oracle\base.py", line 710, in execute 
    return self.cursor.execute(query, self._param_generator(params)) 
DatabaseError: ORA-00904: "RUNABLE_FILTERS"."ID": invalid identifier 

那表在我的數據庫

enter image description here

+0

運行32位的Python 2.7和Django的1.5.2 – shreddish

+0

是DB通過Django的創建'syncdb'命令?如果沒有,請向我們展示您的表格架構。 –

+0

該數據庫是一個遺留的數據庫,並創建和結構與RunableFilters表一樣,我運行inspectdb創建該表的模型..添加表 – shreddish

回答

4

也就是說問題,django期望一個名爲id

Y你可以輕鬆解決它,瞭解整合遺留數據庫:https://docs.djangoproject.com/en/dev/howto/legacy-databases/

對於您的代碼,找到主鍵並通知django它。 Le'ts supose equipment_id可以爲PK作用:

class RunableFilters(models.Model): 
    equipment_id = models.BigIntegerField(primary_key=True) #<-- here 
    filter_file_name = models.CharField(max_length=255, ... 

如果你有複合PK的話,看到這篇文章:https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys

+0

從字面上看,只是嘗試添加equipment_id作爲主鍵,它在我檢查之前正常工作。即將寫出一個答案。感謝您的幫助 – shreddish