2016-05-11 72 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',但對相關模型的引用。