OK so you are explicitly worrying about a one-to-many relationship that should 
be one-to-one, or vice versa, that is, you aren't worried about FKs or 
remote_side setting up the entirely wrong M2O / O2M for a relationship.

However luckily, the only issue with any of this is having that argument 
available and fortunately it looks like I got around to adding info to 
relationship(), so you can check / validate whatever you want, including if the 
"direction" got set up as expected or whatever other things you want to put 
into that info. here's o2o:

from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import event
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session

Base = declarative_base()


class A(Base):
 __tablename__ = "a"

 id = Column(Integer, primary_key=True)
 data = Column(String)
 b = relationship("B", back_populates="a", uselist=False)


class B(Base):
 __tablename__ = "b"
 id = Column(Integer, primary_key=True)
 a_id = Column(ForeignKey("a.id"))
 data = Column(String)
 a = relationship("A", back_populates="b", info={"one_to_one": True})


@event.listens_for(Base, "mapper_configured", propagate=True)
def _validate(mapper, cls):
 for rel in mapper.relationships:
 if "one_to_one" in rel.info:
 assert rel.mapper.attrs[rel.back_populates].uselist == (
 not rel.info["one_to_one"]
 )


e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)

s.add(A(b=B()))
s.commit()




On Wed, Feb 5, 2020, at 5:15 PM, Jonathan Vanasco wrote:
> 
> 
> On Wednesday, February 5, 2020 at 4:50:36 PM UTC-5, Mike Bayer wrote:
>> what conditions would cause the "uselist" to be incorrect?
> 
> 
> If `Foo.bars` declares:
>  uselist=True,
>  back_populates_uselist=False 
> 
> but `Bar.foo` declares:
>  uselist=True,
>  back_populates_uselist=True
> 
> I want to catch `Foo.bars` specified `back_populates_uselist=False`, but the 
> back_populates relationship `Bar.foo` specified `uselist=True`.
> 
> i.e.. each side of the relationship agrees they are in a one-to-many or 
> one-to-one relationship, instead of them independently defining only their 
> side.
> 
> 
> 
> 
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
>  --- 
>  You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
>  To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
>  To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/3219e8a7-63e4-446e-8eef-641b3d86b339%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/3219e8a7-63e4-446e-8eef-641b3d86b339%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/32bddc06-9ddf-4604-83e0-d9a91a971c7c%40www.fastmail.com.

Reply via email to