没有限制唯一匹配列类错误

时间:2014-01-25 23:10:26

标签: sqlalchemy

我遇到了这个问题,我知道这可能与数据库有关,我不确定。除了这一个之外,所有模型表都很好:

class OrderXPerformerxShow(MyMixin, Base):
    __tablename__ = 'order_x_performers_x_show'
    order_id = Column(Integer, ForeignKey('orders.id'), primary_key=True)
    performerXShow_id = Column(Integer, ForeignKey('performer_x_show.id'), primary_key=True)
    amount = Column(Unicode(50))
    performer = relationship('PerformerXShow')

得到了这个错误:

  

CREATE TABLE order_x_performers_x_show(       id SERIAL NOT NULL,       创建没有时区的TIMESTAMP,       在没有时区的情况下修改了TIMESTAMP,       order_id INTEGER NOT NULL,       “performerXShow_id”INTEGER NOT NULL,       金额VARCHAR(50),       PRIMARY KEY(id,order_id,“performerXShow_id”),       FOREIGN KEY(order_id)REFERENCES order(id),       FOREIGN KEY(“performerXShow_id”)REFERENCES performer_x_show(id)   )   2014-01-25 20:03:58,801 INFO [sqlalchemy.engine.base.Engine] [MainThread] {}   2014-01-25 20:03:58,874 INFO [sqlalchemy.engine.base.Engine] [MainThread] ROLLBACK   Traceback(最近一次调用最后一次):   blahblah ....   sqlalchemy.exc.ProgrammingError :(编程错误)没有干扰限制唯一的雀巢巧克力合唱团爸爸们拉巴拉参考文献«performer_x_show»

这是与...相关的。

class PerformerXShow(MyMixin, Base):
    __tablename__ = 'performer_x_show'
    performer_id = Column(Integer, ForeignKey('performers.id'), primary_key=True)
    show_id = Column(Integer, ForeignKey('shows.id'), primary_key=True)
    show = relationship('Show')
    price = Column(Numeric(8, 2), nullable=False)


class Order(MyMixin, Base):
    __tablename__ = 'orders'
    customer_id = Column(Integer, ForeignKey('customers.id'), nullable=False)
    relationship('OrderXPerformerxShow')

混合是这样的:

class MyMixin(object):
    id = Column(Integer, primary_key=True, autoincrement=True)
    created = Column(DateTime())
    modified = Column(DateTime())

    __mapper_args__ = {'extension': BaseExtension()}

1 个答案:

答案 0 :(得分:1)

我认为错误描述非常详细:

  

“没有干草的限制,独特的que compida con las columnas dadas en la   tabla referida«performer_x_show“

问题是,为了使OrderXPerformerxShow.performerXShow_id成为ForeighKey,它必须引用另一个表的unique列(一个主要是唯一的PrimaryKey或一个具有unique约束它。)

原因是PrimaryKey表的performer_x_show是多列的,包含三列:

  • performer_id
  • show_id
  • id

解决方案:仅idprimary key(默认情况下,我会假设),并为其他两列创建UniqueConstraint

class PerformerXShow(MyMixin, Base):
    __tablename__ = 'performer_x_show'
    performer_id = Column(Integer, ForeignKey('performers.id'))
    show_id = Column(Integer, ForeignKey('shows.id'))
    show = relationship('Show')
    price = Column(Numeric(8, 2), nullable=False)

    __table_args__ = ( UniqueConstraint('performer_id', 'show_id'), )