玩框架2.0演进并创建触发器

时间:2012-09-18 18:41:09

标签: playframework-2.0

我一直试图使用Play 2.0.3创建此触发器。如果我手动运行它,它在MySQL中运行得非常好,但是当它尝试从Play运行时它会失败。

delimiter |
    create trigger company_updated before update on company
        for each row begin
            set new.updated = CURRENT_TIMESTAMP;
        end;
    |
delimiter ;

它抛出的错误是:

我们收到以下错误:您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'delimiter |附近使用正确的语法在尝试运行此SQL脚本时,在第1行[ERROR:1064,SQLSTATE:42000]的每一行的公司更新之前创建触发器company_updated:

4 个答案:

答案 0 :(得分:6)

这曾经是一个错误,并已在2.1中修复。 您可以使用双分号来逃避。它在此处记录:http://www.playframework.com/documentation/2.1.0/Evolutions

所以你的代码应该改变如下:

create trigger company_updated before update on company
    for each row begin
        set new.updated = CURRENT_TIMESTAMP;;
end;

答案 1 :(得分:3)

delimiter关键字不是SQL语句,只在mysql命令行客户端中使用。

尝试删除delimiter部分:

create trigger company_updated before update on company
    for each row begin
        set new.updated = CURRENT_TIMESTAMP;
    end;

但正如你所提到的,你可以点击Play bug with the semi-colon :(

答案 2 :(得分:0)

这似乎是我们无法解决的限制。所以,我们目前只是不支持我们的应用程序中的触发器。相反,我们只是覆盖模型的保存/更新方法......

@Override
public void save() {
    this.updated = new Date();
    this.created = this.updated;
    super.save();
}

@Override
public void update() {
    this.updated = new Date();
    super.update();
}

答案 3 :(得分:0)

不需要分隔符语句。 Play会将您的文件拆分为多个查询,一个文件以&#34 ;;"结束。字符。

如果你想创建一个复杂的触发器,你不希望播放在分号后分割你的文件,所以你用双分号&#34 ;;;"来逃避它。

您使用一个分号结束每个查询,并在触发器内使用双分号结束查询。

示例:

DROP TRIGGER IF EXISTS invoice_line_insert;
CREATE TRIGGER invoice_line_insert AFTER INSERT
ON invoice_line FOR EACH ROW
BEGIN
    IF NEW.type = "DELIVERY" THEN
        UPDATE invoice
        SET invoice.etdelivery_amount = invoice.etdelivery_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
    ELSE
        UPDATE invoice
        SET invoice.etexpense_amount = invoice.etexpense_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
    END IF;;
    UPDATE invoice
    SET invoice.vatamount = (NEW.amount * (
                SELECT vat.rate
                FROM vat
                WHERE vat.id_vat = NEW.vat_id_vat
    )) + invoice.vatamount
    WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
    UPDATE invoice
    SET invoice.itamount = invoice.vatamount +
            invoice.etdelivery_amount +
            invoice.etexpense_amount
    WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
END;