获取外键约束冲突

时间:2014-07-15 14:14:47

标签: python sqlalchemy pyramid

当我尝试使用数据填充数据库时,我遇到了外键约束冲突,但是我似乎无法弄清楚我在关系中做错了什么。 (刚刚进入sqlalchemy。)

我有2张桌子。一个叫tInspectionType,另一个叫tSection。它们通过部分ID(tSection.ixSection)与primary key相关联,在tInspectionType表格中我ixSection作为foreign key

这是关系和表格的样子: tSection

class TSection(Base):
    __tablename__ = 'tSection'

    ixSection = Column(Integer, primary_key=True)
    sSection = Column(Unicode(255))

    def __repr__(self):
        rep = ("<TSection(ixSection:{s.ixSection},"
               " sSection={s.sSection!r})>").format(s=self)
        return rep

然后是我的tInspectionType

class TInspectionType(Base):
    __tablename__ = 'tInspectionType'

    ixInspectionType = Column(Integer, primary_key=True)
    ixSection = Column(Integer, ForeignKey('tSection.ixSection'), nullable=False)

    #ixDeviceType = Column(Integer, ForeignKey('tDeviceType.ixDeviceType'), nullable=False)

    sInspectionType = Column(Unicode(255))

    section = relationship('TSection',
                            uselist=False,
                            backref=backref('inspection'))


    def __repr__(self):
        rep = ("<TInspectionType(ixInspectionType:{s.ixInspectionType},"
               " ixSection={s.ixSection},"
               " sInspectionType={s.sInspectionType!r})>").format(s=self)
        return rep

当我尝试按如下方式插入虚拟数据时发生错误:

pts.Base.metadata.create_all(engine)

sections = [pts.TSection(sSection='Generic Header'),
            pts.TSection(sSection='Circuit Breaker Data'),
            pts.TSection(sSection='Trip Unit Data'),
            pts.TSection(sSection='Sensor Data'),
            pts.TSection(sSection='Trip Unit Results'),
            pts.TSection(sSection='Insulation Resistance'),
            pts.TSection(sSection='Contact Resistance'),
            pts.TSection(sSection='Breaker Inspection'),
            pts.TSection(sSection='Cell Inspection'),]

# Inspection Types
bkr_insp_types = [pts.TInspectionType(sInspectionType='Inspect Contacts - Main'),
                  pts.TInspectionType(sInspectionType='Inspect Contacts - Arcing'),
                  pts.TInspectionType(sInspectionType='Inspect Insulators - Bus'),
                  pts.TInspectionType(sInspectionType='Inspect Insulators - Barrier'),
                  pts.TInspectionType(sInspectionType='Clean & Inspect Arc Chutes'),
                  pts.TInspectionType(sInspectionType='Clean Breaker'),
                  pts.TInspectionType(sInspectionType='Racking Mechanism'),
                  pts.TInspectionType(sInspectionType='Manual Close'),
                  pts.TInspectionType(sInspectionType='Electrical Close'),
                  pts.TInspectionType(sInspectionType='Tripping Manual'),
                  pts.TInspectionType(sInspectionType='Tripping Electrical'),
                  pts.TInspectionType(sInspectionType='Tripping Protection'),]

for ins in bkr_insp_types:
    ins.section= sections[7]

我似乎无法看到我出错的地方。不确定它是否在我的表设置中我出错或填充表

1 个答案:

答案 0 :(得分:0)

我没有注意到还有另一个地方我需要分配该部分(之前的关系是一个名为tDeviceType的表格,而且它仍然被分配了一个DeviceType #39; t已经存在于我的表格设置中了) enter image description here

这是正确的代码:

# Inspection Types
bkr_insp_types = [pts.TInspectionType(sInspectionType='Inspect Contacts - Main'),
                  pts.TInspectionType(sInspectionType='Inspect Contacts - Arcing'),
                  pts.TInspectionType(sInspectionType='Inspect Insulators - Bus'),
                  pts.TInspectionType(sInspectionType='Inspect Insulators - Barrier'),
                  pts.TInspectionType(sInspectionType='Clean & Inspect Arc Chutes'),
                  pts.TInspectionType(sInspectionType='Clean Breaker'),
                  pts.TInspectionType(sInspectionType='Racking Mechanism'),
                  pts.TInspectionType(sInspectionType='Manual Close'),
                  pts.TInspectionType(sInspectionType='Electrical Close'),
                  pts.TInspectionType(sInspectionType='Tripping Manual'),
                  pts.TInspectionType(sInspectionType='Tripping Electrical'),
                  pts.TInspectionType(sInspectionType='Tripping Protection'),]

for ins in bkr_insp_types:
    ins.section = sections[0]

cell_insp_types = [pts.TInspectionType(sInspectionType='Inspect Insulators - Barrier'),
                    pts.TInspectionType(sInspectionType='Interlock'),
                    pts.TInspectionType(sInspectionType='Clean Cell'),
                    pts.TInspectionType(sInspectionType='Racking Mechanism'),
                    pts.TInspectionType(sInspectionType='Drawout Rails or Pan'),]

for ins in cell_insp_types:
    ins.section = sections[0]