2010-10-06 28 views
0

數據庫:在OS XColdFusion的ORM創建錯誤的數據類型

ORM設置在的Application.cfc的MySQL 5.1.47:

this.ormEnabled = TRUE; this.ormsettings = { autogenmap =真, dbCreate的= application.dbCreate, automanageSession =真, 數據源= application.dsn, logSQL = application.logSQL, sqlScript = application.sqlScript };

News.cfc

/** 
* These are the news items 
* @persistent true 
* @accessors true 
* @output false 
* @entityname "News" 
* @table news 
*/ 

component 
{ 
property name="NewsId" type="string" fieldtype="id" ormtype="integer" generator="native" generated="insert"; 
property name="Teaser" type="string" sqltype="varchar(200)"; 
property name="Story" type="string" sqltype="varchar(500)"; 
property name="ProductLineId" type="numeric" sqltype="int" ormtype="int" fieldtype="many-to-one" cfc="ProductLine" fkcolumn="ProductLineId" foreignkeyname="fk_productline_news"; 

} 

ProductLine.cfc

/** 
* @persistent true 
* @accessors true 
* @output false 
* @table productline 
*/ 

component 
{ 
property name="ProductLineId" sqltype="int" fieldtype="id" ; 
property name="Label" type="string" sqltype="varchar(50)"; 
} 

從ORMReload輸出調試()

[localhost]:10/05 21:32:00 [jrpp-70] HIBERNATE DEBUG - 
[localhost]: create table news (
[localhost]:  NewsId integer not null auto_increment, 
[localhost]:  Teaser varchar(200), 
[localhost]:  Story varchar(500), 
[localhost]:  **ProductLineId varchar(255)**, 
[localhost]:  primary key (NewsId) 
[localhost]: ) 
[localhost]:10/05 21:32:00 [jrpp-70] HIBERNATE DEBUG - 
[localhost]: create table productline (
[localhost]:  ProductLineId int not null, 
[localhost]:  Label varchar(50), 
[localhost]:  primary key (ProductLineId) 
[localhost]: ) 
[localhost]:10/05 21:32:01 [jrpp-70] HIBERNATE DEBUG - 
[localhost]: alter table news 
[localhost]:  add index fk_productline_news (ProductLineId), 
[localhost]:  add constraint fk_productline_news 
[localhost]:  foreign key (ProductLineId) 
[localhost]:  references productline (ProductLineId) 

,當它試圖創建的外鍵關係的數據庫創建失敗。注意新聞中的字段是一個varchar(255)。那個是從哪裏來的?我試圖在每個我能找到的地方將它設置爲一個整數,但它總是以varchar的形式生成。我認爲這就是爲什麼關係失敗,因爲這兩個字段是不同的數據類型。

我在做什麼錯?

回答

0

下面是我採取的步驟終於得到這個正常工作。

  1. 小心的tablename情況與 的ColdFusion/MySQL的/ OS X.它可以 真的殺了ORM。什麼情況是 ,如果錯誤的情況下,治療是在配置MySQL使用 ,奧姆將 刪除表,但隨後無法 重新創建因爲MySQL仍然 認爲表存在,但有不同的 情況。這可能是 特別令人沮喪,因爲 MySql工具實際上不會 顯示包含錯誤 大小寫的表,但仍可以查詢它。 這是MySql 團隊的已知問題。我的建議是隻爲您的表 提供 所有小寫名稱,並將您的MySql表格案例 配置選項設置爲1(存儲在 小寫字母,不區分大小寫)。那 似乎爲我工作。

  2. 確保你的所有按鍵設置你的話ORM 選項 「MySQLwithInnoDb」

  3. 集ormtype。

  4. 堅持int或整數,並一致使用它。我用整數去了。

  5. 偶爾重啓一切/重新啓動。

  6. 我把SQLTYPE關外鍵引用的News.cfc

我做過的一切之後,終於開始按預期工作。

這是我最後的News.cfc

/** 
* Theser are the news items on the home page. 
* @persistent true 
* @accessors true 
* @output false 
* @entityname "News" 
* @table news 
*/ 

component 
{ 
    property name="NewsId" type="string" fieldtype="id" ormtype="integer" generator="native" generated="insert"; 
    property name="Teaser" type="string" sqltype="varchar(200)"; 
    property name="Story" type="string" sqltype="varchar(500)"; 
    property name="ProductLine" fieldtype="many-to-one" cfc="ProductLine" ormtype="integer" fkcolumn="ProductLineId" foreignkeyname="fk_productline_news"; 

} 
1

在News.cfc試試這個:

屬性名= 「PRODUCTLINE」 字段類型= 「多到一個」 CFC = 「PRODUCTLINE」 fkcolumn = 「ProductLineID」 foreignkeyname = 「fk_productline_news」

+0

而這部作品,並允許出口的CForm完成,關係沒有出現在MySQL的存在。 ProductLineId仍然被創建爲varchar(255)。看起來應該是int – anopres 2010-10-07 01:05:51

0

ProductLine.cfc中的ProductLineID屬性中不需要ormtype嗎?我注意到你在一個地方有「ormtype = int」,而在另一個地方有ormtype = integer。

+0

將ormtype =「int」添加到ProductLine Id似乎修復了News表的數據類型,但仍然沒有創建關係,據我所知。 int和integer應該沒有什麼區別,因爲它們在MySQL中是同義詞,但我會標準化爲一個。 – anopres 2010-10-07 01:11:26

+0

我發現的一件事是,您在News組件的ProductLineID上有一個「type = numeric」。你應該把它關掉。不過,我懷疑它會影響hibernate如何創建表。 – 2010-10-07 18:01:44