Breeze在将其设置为null后为外键属性指定默认值

时间:2012-12-25 16:16:27

标签: breeze

我们仍在使用Northwind数据库测试Breeze,我们遇到了一个奇怪的行为。

首先我几乎可以肯定这不是一个bug,它不可能,它是如此基本的操作。

我们有一个产品实体,我们将其SupplierID设置为null(外键可以为空),

product.SupplierID(null)
在这微风吹过之后,

  1. 将产品的SupplierID值设为null
  2. 将产品供应商设为null
  3. 将产品的SupplierID设为0
  4. ,这会导致外键异常。

    可能这是一个简单的问题,我们不得不错过一些事情,我的一位同事试图从今天早上开始解决这个问题,没有运气。

    他发现在这项任务之后,breeze会将此调用两次(一次针对SupplierID,另一次针对供应商)并将其分配为空值。

    result = ko.computed({
                    read: target,  //always return the original observables value
                    write: function(newValue) {
                        instance._$interceptor(property, newValue, target);
                        return instance;
                    }
                });
    

    在此微风检查外键并执行此行后,

    if (property.relatedDataProperties) {
    if (!entityAspect.entityState.isDeleted()) {
        var inverseKeyProps = property.entityType.keyProperties;
        inverseKeyProps.forEach(function(keyProp, i ) {
            var relatedValue = newValue ? newValue.getProperty(keyProp.name) : keyProp.defaultValue;
            that.setProperty(property.relatedDataProperties[i].name, relatedValue);
        });
    }
    

    有趣的是,此行检查value是否为null(对于product.SupplierID),如果为null,则将其设置为Supplier表的键属性的默认值,它为0(因为它是主键,所以它不可为空)

    我们刚刚更新到0.80.2版本,但仍然是相同的行为。

    提前致谢。

    [UPDATE]

    这是我们的测试,

    test("set foreign key property to null", 3, function () {
        var productQuery = new EntityQuery("Products").take(1)
            .expand("Supplier");
    
        stop();
        queryForOne(newEm, productQuery, "First Product")
            .then(assertProductSetSupplierIDToNull)
            .fail(handleFail)
            .fin(start);
    });
    
    function assertProductSetSupplierIDToNull(data) {
        var products = data.results;
        var firstProduct = products[0];
    
        ok(firstProduct.SupplierID(), "SupplierID is "+firstProduct.SupplierID());
    
        firstProduct.SupplierID(null);
    
        equal(firstProduct.SupplierID(), null, "is SupplierID null?");
    }
    

    结果是,

    另一个有趣的事情是,如果我们像这样设置这个值两次,

        firstProduct.SupplierID(null);
        firstProduct.SupplierID(null);
    

    测试通过,

    enter image description here

    我希望这个示例足以重现这种行为。

    提前致谢。

1 个答案:

答案 0 :(得分:1)

这是一个错误,已在v 0.80.3中修复,可在微风网站上找到。 ...并感谢单元测试;它确实有帮助。

相关问题