Sequelize belongsToMany关联不通过表

时间:2015-04-21 19:45:52

标签: sql node.js postgresql orm sequelize.js

我在PostgreSQL上使用Sequelize来存储属于组织的用户。组织拥有用户可以访问的设备。所以从本质上讲,用户也可以通过他们的组织拥有设备。

我将每个设备与使用organization_id的组织相关联,并且每个用户都使用organization_id与组织相关联。我正试图用Sequelize来设置它以正确阅读它。我正在尽力不去编写自定义查询,但如果我最终还是可以的话。

我正在尝试将所有设备与用户ID相关联。当我尝试运行findAll(...)命令时,Sequelize打印出这个疯狂的查询并出现错误。它输出此查询,然后输出一个空集:

SELECT 
    "receiver"."receiver_id" AS "id", 
    "receiver"."organization_id" AS "organizationID", 
    "receiver"."identifier", 
    "receiver"."secret", 
    "receiver"."iterations", 
    "receiver"."sodium", 
    "receiver"."algorithm", 
    "receiver"."created", 
    "receiver"."modified", 
    "receiver"."deleted", 
    "receiver"."organization_id", 
    "users"."user_id" AS "users.id", 
    "users"."password" AS "users.password", 
    "users"."sodium" AS "users.sodium", 
    "users"."email" AS "users.email", 
    "users"."organization_id" AS "users.organizationID", 
    "users"."iterations" AS "users.iterations", 
    "users"."algorithm" AS "users.algorithm", 
    "users"."created" AS "users.created", 
    "users"."modified" AS "users.modified", 
    "users"."deleted" AS "users.deleted", 
    "users"."organization_id" AS "users.organization_id", 
    "users.organizations"."created" AS "users.organizations.created", 
    "users.organizations"."modified" AS "users.organizations.modified", 
    "users.organizations"."organization_id" AS "users.organizations.organization_id" 
FROM "receivers" AS "receiver" 
INNER JOIN (
    "organizations" AS "users.organizations" 
    INNER JOIN "users" AS "users" 
    ON "users"."user_id" = "users.organizations"."organization_id") 
ON "receiver"."receiver_id" = "users.organizations"."organization_id" 
AND ("users"."deleted" IS NULL AND "users"."user_id" = 2) 
WHERE "receiver"."deleted" IS NULL;

如何更好地编写定义或代码?

非常感谢。

我在Sequelize中的表定义:

var organization = sequelize.define( 'organization', {
    'id': {
        type: Sequelize.BIGINT,
        field: 'organization_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'name' : {
        type:  Sequelize.STRING( 256 ),
        field: 'name',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'deletedAt' : 'deleted',
    'tableName' : 'organizations',
    'paranoid'  : true
} );

var user = sequelize.define( 'user', {
    'id': {
        type: Sequelize.BIGINT,
        field: 'user_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'password': {
        type: Sequelize.STRING( 64 ),
        field: 'password',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'sodium': {
        type: Sequelize.STRING( 64 ),
        field: 'sodium',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'email' : {
        type:  Sequelize.STRING( 64 ),
        field: 'email',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'organizationID' : {
        type:  Sequelize.BIGINT,
        field: 'organization_id',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'iterations' : {
        type:  Sequelize.INTEGER,
        field: 'iterations',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'algorithm' : {
        type:  Sequelize.STRING( 8 ),
        field: 'algorithm',
        allowNull: false,
        defaultValue: 'sha256'
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'deletedAt' : 'deleted',
    'tableName' : 'users',
    'paranoid'  : true
} );

var receiver = sequelize.define( 'receiver', {
    'id': {
        type: Sequelize.BIGINT,
        field: 'receiver_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'organizationID': {
        type: Sequelize.BIGINT,
        field: 'organization_id',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'identifier': {
        type: Sequelize.STRING( 64 ),
        field: 'identifier',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'secret' : {
        type:  Sequelize.STRING( 64 ),
        field: 'secret',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'iterations' : {
        type:  Sequelize.INTEGER,
        field: 'iterations',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'sodium': {
        type: Sequelize.STRING( 64 ),
        field: 'sodium',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'algorithm' : {
        type:  Sequelize.STRING( 8 ),
        field: 'algorithm',
        allowNull: false,
        defaultValue: 'sha256'
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'deletedAt' : 'deleted',
    'tableName' : 'receivers',
    'paranoid'  : true
} );

// Organizations have users and users have organizations
organization.hasMany( user, { 'foreignKey' : 'organization_id' } );
user.belongsTo( organization, { 'foreignKey' : 'organization_id' } );

// Organizations have receivers
organization.hasMany( receiver, { 'foreignKey' : 'organization_id' } );
receiver.belongsTo( organization, { 'foreignKey' : 'organization_id' } );

// Receivers to users
user.belongsToMany( receiver, { 'through' : 'organizations', 'foreignKey' : 'organization_id' } );
receiver.belongsToMany( user, { 'through' : 'organizations', 'foreignKey' : 'organization_id' } );

我用来查询的代码:

// Get the devices for this person
db.receiver.findAll( {
    'include' : [
                {
                    'model' : db.user,
                    'where' : { 'id' : 2 }
                }
            ]
} )
.complete( function( error, result ) {
    if( error ) {
        console.log( error );
    }
    else {
        console.log( result );
    }
} );

1 个答案:

答案 0 :(得分:2)

尝试以下操作,它会选择与where语句匹配的用户,并包含与之关联的组织,其中包含与之关联的设备,因此您最终应该使用与之关联的设备用户。

// Organizations have users
user.belongsTo(organization);
// Organizations have receivers
receiver.belongsTo(organization);


// Get the devices for this person
db.user.find( {
    'include' : [
        {model: db.organization,
         include: [model: db.receiver]
        }
    ]
    'where' : { 'id' : 2 }
} )
.complete( function( error, result ) {
    if( error ) {
        console.log( error );
    }
    else {
        console.log( result );
    }
} );

如果你使用下划线id字段名称,例如organization_id,你可以指定" underscored:true"在创建模型时,您不必在创建关联时指定外键字段。

相关问题