2016-05-11 42 views
0

我对帆两种型号引用(外键)在sailsjs

1. User_type 
2. User 
  1. USER_TYPE型号:

    module.exports = { 
    schema: true, 
    connection: 'mongo', 
    attributes: { 
    
        user_type: { 
        type: 'string', 
        required: true, 
        enum: ['superadmin', 'admin', 'follower'] 
    },  
    
    toJSON() { 
    return this.toObject(); 
    } 
    }, 
    
    beforeUpdate: (values, next) => next(), 
    beforeCreate: (values, next) => next() 
    }; 
    
  2. 用户模型:

    module.exports = { 
        schema: true, 
    
        attributes: { 
    
    
        fname: { 
         type: 'string', 
         required: true, 
        },  
    
        user_login_type: { 
         // This user_login_type should be having values from 
         // User_type Model with a reference to field 'user_type', that is 
         // whatever values(superadmin', 'admin', 'follower') are in the 
         // 'user_type' of collection(or model) 'User_type' should only be 
         // allowed to enter here, and no else value 
        },  
    
    
        toJSON() { 
         return this.toObject(); 
        } 
        }, 
    
        beforeUpdate: (values, next) => next(), 
        beforeCreate: (values, next) => next() 
    }; 
    

我提到不同的文档,问题和答案,但我没有得到确切的流动,如何在帆

每个帮助做到这一点实在是可观

回答

0

看一看associations。您可以使用model属性创建引用:

... 
user_login_type: { 
    model: 'User_type' 
}, 
... 

添加required: true,如果你想让它强制性的。

注意的user_login_type值不会是'superadmin'之一,'admin''follower',但对相关模型的引用。