2013-10-27 116 views
0

我有一個由doctrine/symfony1.4生成的mysql數據庫,我需要一個單獨的django 1.4項目來引用同一個mysql數據庫上的現有表。數據庫由許多面向客戶端的用戶界面共享,無法進行遷移或複製,symfony和django都需要引用/更新同一數據庫上的相同表,以便面向客戶端的UI同步。不幸的是,doctrine在那裏,並且在這些表之間創建了約束名稱和索引名稱,我找不到如何強制django使用相同的名稱。當我嘗試在django對象中包裝這些現有表時,這會導致崩潰。Django:更改外鍵約束名稱

任何想法如何覆蓋或強制django 1.4模型使用特定的外鍵約束名稱以及索引?

更新:增加有關表設置和崩潰的詳細信息

主義模式:

Feature: 
    actAs: [Timestampable] 
    columns: 
     name: { type: string(100) } 
     description: { type: string(200) } 
     pivotXpos: { type: float, notnull: true } 
     pivotYpos: { type: float, notnull: true } 
     referenceImageUrl: { type: string(200), notnull: true } 
     referenceThumbnailImageUrl: { type: string(200), notnull: true } 
     bbox_minx: { type: float, notnull: true } 
     bbox_miny: { type: float, notnull: true } 
     bbox_maxx: { type: float, notnull: true } 
     bbox_maxy: { type: float, notnull: true } 
    relations: 
     Curve: 
     local: id 
     foreign: feature_id 
     cascade: [delete] 
     type: many 


Curve: 
    actAs: [Timestampable] 
    columns: 
     name: { type: string(100), notnull:true} 
     feature_id: { type: bigint, notnull: true } 
    relations: 
     Feature: { onDelete: CASCADE, local: feature_id, foreign: id, 
       foreignAlias: Curves } 

表SQL

CREATE TABLE feature (id BIGINT AUTO_INCREMENT, 
name VARCHAR(100), description VARCHAR(200), 
pivotxpos FLOAT(18, 2) NOT NULL, 
pivotypos FLOAT(18, 2) NOT NULL, 
referenceimageurl VARCHAR(200) NOT NULL, 
referencethumbnailimageurl VARCHAR(200) NOT NULL, 
bbox_minx FLOAT(18, 2) NOT NULL, 
bbox_miny FLOAT(18, 2) NOT NULL, 
bbox_maxx FLOAT(18, 2) NOT NULL, 
bbox_maxy FLOAT(18, 2) NOT NULL, 
created_at DATETIME NOT NULL, 
updated_at DATETIME NOT NULL, 
PRIMARY KEY(id)) ENGINE = INNODB; 

CREATE TABLE curve (id BIGINT AUTO_INCREMENT, 
name VARCHAR(100) NOT NULL, 
feature_id bigint NOT NULL, 
created_at DATETIME NOT NULL, 
updated_at DATETIME NOT NULL, 
INDEX feature_id_idx (feature_id), 
PRIMARY KEY(id)) ENGINE = INNODB; 

ALTER TABLE curve ADD CONSTRAINT curve_feature_id_feature_id 
FOREIGN KEY (feature_id) REFERENCES feature(id) ON DELETE CASCADE; 

的Django類:

class Feature(models.Model): 

    class Meta: 
     db_table = 'feature' 

    name = models.CharField(max_length=100) 
    created_at = models.DateTimeField(auto_now_add=True) 
    updated_at = models.DateTimeField(auto_now=True) 
    description = models.CharField(max_length=100) 
    pivotXpos = models.FloatField() 
    pivotYpos = models.FloatField() 
    pivotXpos = models.FloatField() 
    referenceImageUrl = models.CharField(max_length=200) 
    referenceThumbnailImageUrl = models.CharField(max_length=200) 
    bbox_minx = models.FloatField() 
    bbox_miny = models.FloatField() 
    bbox_maxx = models.FloatField() 
    bbox_maxy = models.FloatField() 

    def __unicode__(self): 
     return self.name; 


class Curve(models.Model): 

    class Meta: 
     db_table = 'curve' 

    name = models.CharField(max_length=100) 
    created_at = models.DateTimeField(auto_now_add=True) 
    updated_at = models.DateTimeField(auto_now=True) 
    feature_id = models.ForeignKey(Feature) 

    def __unicode__(self): 
     return self.name; 

崩潰(Django的外殼內)

>>> Curve.objects.all() 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/models/query.py", line 72, in __repr__ 
    data = list(self[:REPR_OUTPUT_SIZE + 1]) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/models/query.py", line 87, in __len__ 
    self._result_cache.extend(self._iter) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/models/query.py", line 291, in iterator 
    for row in compiler.results_iter(): 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 763, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/backends/util.py", line 40, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 114, in execute 
    return self.cursor.execute(query, args) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute 
    self.errorhandler(self, exc, value) 
    File "/Users/a/Documents/web/impression/djangodev/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
DatabaseError: (1054, "Unknown column 'curve.feature_id_id' in 'field list'") 
+0

什麼是「崩潰」的確切性質?我不清楚爲什麼不同的索引名稱和約束名稱會影響Django ORM。 –

回答

0

有關約束/索引名不知道,但我可以用在ForeignKeyField

的「db_column」字段選項來解決這個問題
feature_id = models.ForeignKey(Feature,db_column='feature_id') 
1

這是錯誤: (1054, 「未知列在 '字段列表' curve.feature_id_id'」) 所以,如果你不想指定db_column作爲user257543設, 你可以寫,而不是

feature = models.ForeignKey(Feature) 

這是更清潔