Proper way to define two way relations between two tables

家住魔仙堡 提交于 2019-12-11 23:37:41

问题


I have three tables:

Employee (emp_id,name)
Projects (pid,proj_status)
Project_Allocation (pid,emp_id)

To find the people belonging to my project:
1. I have my name, I query my emp_id.
2. Using my emp_id, I query (withRelated) for all my projects from Project_Allocation (completed ones as well) with a belongsTo relation defined on Projects
3. Then I pick the pid of the project having status as 'LIVE'
4. Now I pick all the models having this pid in Project_allocation.

The issue I'm encountering is when creating the schemas for Project and Project_Allocation tables. The both require each other and I cant load them both. Please suggest how I can rewrite this.

Here are few extracts:

The Project_Allocation file:

var Project = require('./project');
var PASchema = Bookshelf.Model.extend({
tableName: 'project_allocation',
project: function(){
    return this.belongsTo(ProjectSchema, 'pid')
}

The Project file:

var ProjAlloc = require('./projAlloc');
var ProjectSchema = Bookshelf.Model.extend({
tableName: 'project',
idAttribute: 'pid',
allocatedIDs: function() {
    return this.belongsTo(PASchema,'pid')
}

回答1:


The problem was with require. The deadlock was being caused there. I was defining each of my table in a separate file and require-ing them in other table definitions (i.e other files). Writing all my table definitions in a single file and then exporting that object worked fine.



来源:https://stackoverflow.com/questions/24866820/proper-way-to-define-two-way-relations-between-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!