Breeze-Sequelize with autoGeneratedKeyType Identity

时间:2015-07-29 15:13:22

标签: javascript node.js breeze sequelize.js

我正在尝试使用breeze-breeze sequelize创建一个MS SQL数据库,我想在数据库服务器上生成ID。我的解决方案是针对微风样本repo

中的tempHire示例

我的Metadata.json看起来像这样:

{
    "metadataVersion": "1.0.5",
    "namingConvetion": "camelCase",
    "localQueryComparisonOptions": "caseInsensitiveSQL",
    "dataServices": [{
        "serviceName": "breeze/",
        "hasServerMetadata": true,
        "useJsonp": false
    }],
    "structuralTypes": [{

        "shortName": "User",
        "namespace": "Model",
        "autoGeneratedKeyType": "Identity",
        "defaultResourceName": "Users",
        "dataProperties": [{
            "nameOnServer": "id",
            "dataType": "Int32",
            "isPartOfKey": true,
            "isNullable": false
        }, {
            "name": "firstName",
            "dataType": "String"
        }, {
            "name": "lastName",
            "dataType": "String"
        }, {
            "name": "userName",
            "dataType": "String",
            "isNullable": false,
            "maxLength": 64,
            "validators": [{
                "name": "required"
            }, {
                "maxLength": 64,
                "name": "maxLength"
            }]
        }, {
            "name": "email",
            "dataType": "String"
        }]
    }],
    "resourceEntityTypeMap": {
        "Users": "User:#Model"
    }
}

虽然这不会创建身份标识列。 创建的表看起来像以下创建脚本:

CREATE TABLE [User] (
    [id] INTEGER NOT NULL , 
    [firstName] NVARCHAR(255) DEFAULT NULL, 
    [lastName] NVARCHAR(255) DEFAULT NULL, 
    [userName] NVARCHAR(64) NOT NULL DEFAULT '', 
    [email] NVARCHAR(255) DEFAULT NULL, 
    PRIMARY KEY ([id])
)

此外还有一些breeze服务器端实现:

var dbConfig = {
    user: 'user',
    password: 'secret',
    dbName: 'dbname'
};

var sequelizeOptions = {
    host: 'hostname',
    dialect: 'mssql',
    port: 1433
};

function createSequelizeManager() {
    var metadata = readMetadata();
    var sm = new SequelizeManager(dbConfig, sequelizeOptions);
    sm.importMetadata(metadata);

    return sm;
}

var _sequelizeManager = createSequelizeManager();

_sequelizeManager.authenticate();

_sequelizeManager.sync(false /* createDb */)
    .then(seed)
    .then(function () {
        console.log('db init successful');
    });

我配置错误吗?身份不适用于mssql方言吗?难道我做错了什么?

1 个答案:

答案 0 :(得分:3)

配置没有错,我猜。 我刚刚从breeze-sequelize中发现MetadataMapper中有一个错误。我用续集版本2.1.3和3.x进行了测试。

sequelize的autoIncrement属性永远不会被设置。 if语句永远不会成立。我将在github上报告此事。 ;)

修复将是MetadataMapper.js134处的以下代码:

  if (attributes.type.key == "INTEGER" || attributes.type.key =="BIGINT") {
    attributes.autoIncrement = true;
  }

在原始代码中,if语句是attributes.type== "INTEGER" || attributes.type=="BIGINT",其中类型实际上从不是字符串。