title: SQLALCHEMY/FASTAPI/POSTGRESQL | Only retrieve double entries
body: <p>I have a database with a table named <code>friends</code>. That table has two columns, <code>&quot;user_id&quot; and &quot;friend_id&quot;</code>.
  Those are foreign keys from the <code>Users</code> table.</p>
  <p>My friends table right now:</p>
  <pre><code>               user_id               |              friend_id
  -------------------------------------+-------------------------------------
   google-oauth2|11539665289********** | google-oauth2|11746442253**********
   google-oauth2|11746442253********** | google-oauth2|11539665289**********
   google-oauth2|11746442253********** | google-oauth2|11111111111**********
  </code></pre>
  <p>The first two rows are the same IDs but flipped. Those <code>Users</code> I want to retrieve, because they added eachother. The third row only added another guy, that one shouldn't be retrieved.</p>
  <p>My SQLModels (models.py):</p>
  <pre><code>class Friends(SQLModel, table=True):
      __tablename__ = &quot;friends&quot;
      user_id: str = Field(sa_column=Column('user_id', VARCHAR(length=50), primary_key=True), foreign_key=&quot;users.id&quot;)
      friend_id: str = Field(sa_column=Column('friend_id', VARCHAR(length=50), primary_key=True), foreign_key=&quot;users.id&quot;)
  
  class UserBase(SQLModel):
      id: str
      username: Optional[str]
      country_code: Optional[str]
      phone: Optional[str]
      picture: Optional[str]
  
      class Config:
          allow_population_by_field_name = True
  
  
  class User(UserBase, table=True):
      __tablename__ = 'users'
      id: str = Field(primary_key=True)
      username: Optional[str] = Field(sa_column=Column('username', VARCHAR(length=50), unique=True, default=None))
      phone: Optional[str] = Field(sa_column=Column('phone', VARCHAR(length=20), unique=True, default=None))
      picture: Optional[str] = Field(sa_column=Column('picture', VARCHAR(length=255), default=None))
  </code></pre>
  <p>My fastapi endpoint:</p>
  <pre><code>@router.get(&quot;&quot;, status_code=status.HTTP_200_OK, response_model=models.FriendsList, name=&quot;Get Friends for ID&quot;,
              tags=[&quot;friends&quot;])
  async def get_friends(
          user_id: str = Query(default=None, description=&quot;The user_id that you want to retrieve friends for&quot;),
          session: Session = Depends(get_session)
  ):
      stm = select(models.User, models.Friends).where(models.User.id == models.Friends.friend_id, models.Friends.user_id == user_id)
      res = session.exec(stm).all()
      if not res:
          raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                              detail=&quot;There are no friendships associated with this id.&quot;)
      users = []
      for item in res:
          users.append(item[0])
      return models.FriendsList(users=users)
  </code></pre>
  <p>My code works perfectly fine, only the query needs to be replaced.</p>
  <pre><code>stm = select(models.User, models.Friends).where(models.User.id == models.Friends.friend_id, models.Friends.user_id == user_id)
  res = session.exec(stm).all()
  </code></pre>
  <p>This query returns every <code>User</code> that has the given ID as <code>user_id</code>, but doesn't check if there is an entry the other way around.</p>
  <p>Example for what I want to get:</p>
  <p>I make a GET request to my endpoint with the id <code>google-oauth2|11746442253**********</code>. I would get the User <code>google-oauth2|11539665289**********</code>. (The User <code>google-oauth2|11111111111**********</code> would not be retrieved because there is no entry the other way arround)</p>
  <p>I hope you guys understand my problem. If there are any questions feel free to ask.</p>
  <p>Best regards,
  Colin</p>