I am mapping classes to existed MySQL tables 、I need to relate to tables(one-to-one), but I’ve got a problem、Two tables have the column nid、Both fields are primary keys、I can not define foreign key without a primary key、And relation map is not applied to a primary key、My version is below.
class Node(base): __tablename__ = 'node' nid = Column(Integer, primary_key=True) vid = Column(Integer) uuid = Column(String(128)) type = Column(String) field_data = relationship("NodeFieldData", order_by="NodeFieldData.nid", backref="node") def __repr__(self): return "
解决方式
nid = Column(Integer, ForeignKey(Node.nid), primary_key=True)
base = declarative_base()class Node(base): __tablename__ = 'node' nid = Column(Integer, primary_key=True) uuid = Column(String(128)) vid = Column(Integer) type = Column(String) # Refferer. field_data = relationship('NodeFieldData', backref="node", uselist=False) def __repr__(self): return "