mirror of
				https://github.com/fastapi/sqlmodel.git
				synced 2025-10-31 01:58:00 +08:00 
			
		
		
		
	 fa2f178b8a
			
		
	
	fa2f178b8a
	
	
	
		
			
			Co-authored-by: Mohamed Farahat <farahats9@yahoo.com> Co-authored-by: Stefan Borer <stefan.borer@gmail.com> Co-authored-by: Peter Landry <peter.landry@gmail.com> Co-authored-by: Anton De Meester <antondemeester+github@gmail.com>
		
			
				
	
	
		
			54 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from typing import List, Optional
 | |
| 
 | |
| import pytest
 | |
| from sqlalchemy.orm import relationship
 | |
| from sqlmodel import Field, Relationship, SQLModel
 | |
| 
 | |
| 
 | |
| def test_sa_relationship_no_args() -> None:
 | |
|     with pytest.raises(RuntimeError):  # pragma: no cover
 | |
| 
 | |
|         class Team(SQLModel, table=True):
 | |
|             id: Optional[int] = Field(default=None, primary_key=True)
 | |
|             name: str = Field(index=True)
 | |
|             headquarters: str
 | |
| 
 | |
|             heroes: List["Hero"] = Relationship(
 | |
|                 back_populates="team",
 | |
|                 sa_relationship_args=["Hero"],
 | |
|                 sa_relationship=relationship("Hero", back_populates="team"),
 | |
|             )
 | |
| 
 | |
|         class Hero(SQLModel, table=True):
 | |
|             id: Optional[int] = Field(default=None, primary_key=True)
 | |
|             name: str = Field(index=True)
 | |
|             secret_name: str
 | |
|             age: Optional[int] = Field(default=None, index=True)
 | |
| 
 | |
|             team_id: Optional[int] = Field(default=None, foreign_key="team.id")
 | |
|             team: Optional[Team] = Relationship(back_populates="heroes")
 | |
| 
 | |
| 
 | |
| def test_sa_relationship_no_kwargs() -> None:
 | |
|     with pytest.raises(RuntimeError):  # pragma: no cover
 | |
| 
 | |
|         class Team(SQLModel, table=True):
 | |
|             id: Optional[int] = Field(default=None, primary_key=True)
 | |
|             name: str = Field(index=True)
 | |
|             headquarters: str
 | |
| 
 | |
|             heroes: List["Hero"] = Relationship(
 | |
|                 back_populates="team",
 | |
|                 sa_relationship_kwargs={"lazy": "selectin"},
 | |
|                 sa_relationship=relationship("Hero", back_populates="team"),
 | |
|             )
 | |
| 
 | |
|         class Hero(SQLModel, table=True):
 | |
|             id: Optional[int] = Field(default=None, primary_key=True)
 | |
|             name: str = Field(index=True)
 | |
|             secret_name: str
 | |
|             age: Optional[int] = Field(default=None, index=True)
 | |
| 
 | |
|             team_id: Optional[int] = Field(default=None, foreign_key="team.id")
 | |
|             team: Optional[Team] = Relationship(back_populates="heroes")
 |