Hello,

I am a bit puzzled over modeling One-to-One relationships. The example in 
the documentation 
<http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one> 
says to use uselist flag 
<http://docs.sqlalchemy.org/en/latest/orm/relationship_api.html#sqlalchemy.orm.relationship.params.uselist>
 
on the relationship declaration. That does make sense to ensure scalars on 
both sides of the relationship. However, that's just an ORM flag and does 
not necessarily translate to the DB (e.g. using Alembic).

In this context I miss the mention of a unique constraint 
<http://docs.sqlalchemy.org/en/latest/core/constraints.html#unique-constraint>. 
Could the uselist flag not be derived if a unique constraint was specified 
on the foreign key column? For example:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'), unique=True)
    child = relationship("Child", back_populates="parent")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent = relationship("Parent", back_populates="child") # uselist=False 
redundant?

Here, the Parent.child_id column has a unique constraint which narrows the 
Many-to-One to a One-to-One relationship. Would this not make the uselist 
flag redundant?

Or am I missing something?

Thanks!
Jens

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to