小编wil*_*.am的帖子

save方法中的django回滚事务

我有以下代码覆盖模型的save方法:

@transaction.commit_on_success
def save(self, *args, **kwargs):

    try:
        transaction.commit()
        self.qa.vote_down_count += 1
        self.qa.save()

        super(self.__class__, self).save(*args, **kwargs)

    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()
Run Code Online (Sandbox Code Playgroud)

预期的行为是:self.qa属性vote_down_count加1,但如果在super(self)save方法中发生任何异常,则事务回滚(这意味着self.qa.vote_down_count + = 1未在数据库中提交) ).

实际行为是:即使IntegrityError异常从super(self)save引发,self.qa.vote_down_count + = 1也会提交到数据库.

有没有?

python django

5
推荐指数
1
解决办法
5219
查看次数

无效的yii关系HAS_MANY

我在Yii中建立一个简单的民意调查应用程序.

我有以下表格:

create table poll (
    id integer not null auto_increment,
    title varchar(255) not null,
    views integer not null default 0,
    created_at timestamp not null default NOW(),
    PRIMARY KEY(id)
);

create table choice (
    poll_id integer not null,
    choice varchar(200) not null,
    votes integer not null default 0
);
Run Code Online (Sandbox Code Playgroud)

我有一个ActiveRecord for Poll定义为:

class Poll extends CActiveRecord
{
...
    public function relations()
    {
        return array(
            'choices'=>array(self::HAS_MANY, 'Choice', 'poll_id'),
        );
    }
...
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用以下代码时:

$p = Poll::model()->findByPk($id)->with('choices')->findAll();
Run Code Online (Sandbox Code Playgroud)

它给了我追溯:

Invalid argument supplied for …
Run Code Online (Sandbox Code Playgroud)

relationship yii

2
推荐指数
1
解决办法
5239
查看次数

标签 统计

django ×1

python ×1

relationship ×1

yii ×1