开发者

CakePHP calling seq.currval on Oracle when no sequence exists

When trying to insert a record into a table, I get sequence error:

Query: SELECT my_table_seq.currval FROM dual
Warning (512): SQL Error: ORA-02289: sequence does not exist

There is no sequence on this table. It is more like a HABTM. I have a table:

CREATE TABLE "MYSCHEMA"."MY_TABLE"
(
    "WORK_ID"   NUMBER NOT NULL ENABLE,
    "ITEM_ID" NUMBER NOT NULL ENABLE,
    CONSTRAINT "MY_TABLE_PK" PRIMARY KEY ("WORK_ID", "ITEM_ID")
)

I manually add both IDs using:

$this->MyModel->set(array('work_id' => 1, 'item_id' => 2));
$this->开发者_JAVA百科MyModel->save();

Any ideas how to prevent the error? Do I just need to do $this->MyModel->query() and run the query manually?


CakePHP expects that a join table (as part of HABTM relationship) has its own primary key.

See http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

In your case:

CREATE TABLE MY_TABLE
(
    ID        NUMBER NOT NULL,
    WORK_ID   NUMBER NOT NULL,
    ITEM_ID   NUMBER NOT NULL,
    CONSTRAINT MY_TABLE_PK PRIMARY KEY (ID),
    CONSTRAINT MY_TABLE_UK UNIQUE (WORK_ID, ITEM_ID)
)

And don't forget to add the MY_TABLE_SEQ sequence:

CREATE SEQUENCE MY_TABLE_SEQ;


what happens is that when cake calls a lastinsertid for oracle table, it queries the sequence for that table. if no sequence is provided, it just assumes a $tablename + _seq.

what you can do is that tell cakephp at the model what sequence you are using for that table.

MyModel.php

class MyModel extends AppModel{
//in my case 
//my table name was so long it required a shorter sequence name :D
var $sequence = 'my_table_seq';
...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜