【问题标题】:ColdFusion ORM creates wrong data typeColdFusion ORM 创建错误的数据类型
【发布时间】:2010-10-06 01:47:30
【问题描述】:

数据库:OS X 上的 MySql 5.1.47

Application.cfc 中的 ORM 设置:

this.ormEnabled = true; 这个.ormsettings = { autogenmap = 真, dbCreate = application.dbCreate, 自动管理会话 = 真, 数据源 = application.dsn, logSQL = 应用程序.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)

尝试创建外键关系时,数据库创建失败。请注意,news 中的字段是 varchar(255)。那个是从哪里来的?我试图在我能找到的每个地方都将它设置为整数,但它总是生成为 varchar。我认为这就是关系失败的原因,因为这两个字段是不同的数据类型。

我做错了什么?

【问题讨论】:

    标签: orm coldfusion


    【解决方案1】:

    在 News.cfc 中试试这个:

    property name="productLine" fieldtype="many-to-one" cfc="ProductLine" fkcolumn="ProductLineID" foreignkeyname="fk_productline_news"

    【讨论】:

    • 虽然这有效并允许 cform 导出完成,但 MySQL 中似乎不存在这种关系。 ProductLineId 仍创建为 varchar(255)。看起来应该是int
    【解决方案2】:

    您不需要 ProductLine.cfc 中的 ProductLineID 属性的 ormtype 吗?我注意到你在一个地方有“ormtype=int”,在另一个地方有 ormtype=integer。

    【讨论】:

    • 将 ormtype="int" 添加到 ProductLine Id 似乎确实修复了 News 表的数据类型,但据我所知,关系仍未创建。 int 与 integer 应该没有区别,因为它们在 MySQL 中是同一事物的同义词,但我将对其进行标准化。
    • 我注意到的一件事是新闻组件中的 ProductLineID 上有一个“type=numeric”。你应该把它取下来。但是,我怀疑它会影响 hibernate 创建表的方式。
    【解决方案3】:

    这是我为最终使其正常工作而采取的步骤。

    1. 注意表名大小写 ColdFusion/MySql/OS X. 它可以 真的杀了orm。发生的事情是 如果错误的病例处理是 用于配置MySql,Orm会 放下一张桌子,但随后无法 重新创建它,因为 MySql 仍然 认为该表存在但带有 不同的情况。这可以是 特别令人沮丧的是 MySql 工具实际上不会 显示错误的表格 案例,但您仍然可以查询它。 这是 MySql 的一个已知问题 团队。我的建议是只提供 表的所​​有小写名称 并设置您的 MySql 表案例 配置选项为 1(存储在 小写,不区分大小写)。那 似乎对我有用。

    2. 确保您设置了您的方言 orm “MySQLwithInnoDb”选项

    3. 在所有键上设置 ormtype。

    4. 坚持使用 int 或 integer 并始终如一地使用它。我选择了整数。

    5. 偶尔重启一切/重启。

    6. 我从 News.cfc 中的外键引用中去掉了 sqltype

    在我做了所有这些之后,它终于开始按预期工作了。

    这是我最后的 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";
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2016-10-18
      相关资源
      最近更新 更多