不同列中的一对多关系

时间:2019-01-06 06:56:01

标签: sql postgresql flask sqlalchemy

这是我难以找到明确答案的newbee SQL问题。所以请帮忙。我的数据库中有两个表,其中的字段如下所示:

ConstructionProjects

  • id(唯一,非空)
  • 开发者(一个非空)
  • main_contractor(一个,非空)
  • 建筑师(一个,非空)
  • (其他字段)

公司

  • id(唯一,非空)
  • projects_developed(很多或没有)
  • projects_as_main_contractor(很多或没有)
  • projects_as_architect(很多或没有)
  • (其他字段)

因此,每个项目只有一名开发人员,一名建筑师和一名承包商,但是,可能是同一家公司。任何公司都可以以任何角色参与尽可能多的项目。

有没有一种方法可以避免创建3个其他关联表来建立多对多关系?并建立3个一对多的关系? 如果是这样,哪种做法更好?

*换句话说,我不理解关系(一对多和多对多)将(1)行与行或(2)行与“特定单元格”相关?

  • (1)如果一行一行,那么我有很多对很多的关系
  • (2)如果行到特定单元格,则它是多个一对多关系... *

我正在学习Flask_alchemy和PostgreSQL。 我遇到了麻烦,编写了这样的代码(表之间没有对特定列的引用)。所以这不行吗?

class Company(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    constr_projects_developed = db.relationship('ConstrProject', backref='developer') 
    constr_projects_main_contracts = db.relationship('ConstrProject', backref='main_contractor')
    constr_projects_architect = db.relationship('ConstrProject', backref='architect')

class ConstrProject(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    developer_id = db.Column(db.Integer, db.ForeignKey('company.id'))    
    main_contractor_id = db.Column(db.Integer, db.ForeignKey('company.id'))
    architect_id = db.Column(db.Integer, db.ForeignKey('company.id'))

然后我的问题是,正确的方法是这样的(1):

class Company(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    constr_projects_developed = db.relationship('ConstrProject', back_populates='developer') 
    constr_projects_main_contracts = db.relationship('ConstrProject', back_populates='main_contractor')
    constr_projects_architect = db.relationship('ConstrProject', back_populates='architect')

class ConstrProject(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    developer_id = db.Column(db.Integer, db.ForeignKey('company.id'))    
    developer = db.relationship('Company', back_populates='constr_projects_developed')
    main_contractor_id = db.Column(db.Integer, db.ForeignKey('company.id'))
    main contractor = db.relationship('Company', back_populates='constr_projects_main_contracts')
    architect_id = db.Column(db.Integer, db.ForeignKey('company.id'))
    architect = db.relationship('Company', back_populates='constr_projects_architect')

还是这样(2)?:

class Company(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    cp_developed = db.relationship('Company', secondary=cp_developer_company, back_populates='developer')
    cp_main_contracts = db.relationship('Company', secondary=cp_main_contractor_company, back_populates='main_contractor')
    cp_architects = db.relationship('Company', secondary=cp_architect_company, back_populates='architect')

class ConstrProject(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    developer = db.relationship('Company', secondary=cp_developer_company, back_populates='cp_developed')
    main_contractor = db.relationship('Company', secondary=cp_main_contractor_company, back_populates='cp_main_contracts')
    architect = db.relationship('Company', secondary=cp_architect_company, back_populates='cp_architects')

cp_developer_company = db.Table('cp_developer_company'
    db.Column('company_id', db.Integer, db.ForeignKey('company.id'))
    db.Column('constr_project_id', db.Integer, db.ForeignKey('constrproject.id'))
    )

cp_main_contractor_company = db.Table('cp_main_contractor_company'
    db.Column('company_id', db.Integer, db.ForeignKey('company.id'))
    db.Column('constr_project_id', db.Integer, db.ForeignKey('constrproject.id'))
    )

cp_architect_company = db.Table('cp_architect_company'
    db.Column('company_id', db.Integer, db.ForeignKey('company.id'))
    db.Column('constr_project_id', db.Integer, db.ForeignKey('constrproject.id'))

0 个答案:

没有答案