On Wed, Sep 6, 2017 at 3:12 AM, <jens.troe...@gmail.com> wrote: > Hello, > > I am a bit puzzled over modeling One-to-One relationships. The example in > the documentation says to use uselist flag 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. 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?
One can build a one-to-one relationship *without* a unique constraint being present, hence the uselist flag as a public accessor still has a purpose. Additionally, if your Parent class linked to Child using a composite foreign key where only some of the columns had unique=True, that again means having uselist as explicit is useful. Add to that, sometimes the table has been reflected, and it's only in the last few years that SQLAlchemy has had the ability to reflect unique constraints, and even then, not on all backends. So there's many cases where a unique constraint may or may not be present, yet we still need the uselist flag. None of these cases are the very specific case you have here. The precedent for uselist setting itself to false implicitly would be that a standard many-to-one, which involves foreign keys that link directly to a primary key, and the relationship() construct performs a lot of heuristics when it is configured in order to figure this out. These heuristics took years to get right. Overall, it is optional from an ORM point of view to set unique constraints on Table metadata, even if the backing database does have those constraints in place. But this is not the case for a primary key, which is explicitly required by the ORM. So it is easier for the ORM to rely upon making decisions for uselist based on a primary key constraint, which is a very reliable source of information, versus the presence of a unique constraint, which is an unreliable source of information (in that it is optional, might not be set, might not be reflected, might be present in the database and/or the model and not actually on the other side). So to minimize confusion and try to stick to the principle of "There should be one-- and preferably only one --obvious way to do it.", there is no feature right now to attempt to auto-determine uselist based on the combination of the presence of a unique constraint combined with the correct primaryjoin geometry. > > 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. -- 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.