mirror of
				https://github.com/fastapi/sqlmodel.git
				synced 2025-11-01 02:43:22 +08:00 
			
		
		
		
	 717594ef13
			
		
	
	717594ef13
	
	
	
		
			
			* ♻️ Make sa_column exclusive, do not allow incompatible arguments, sa_column_args, primary_key, etc * ✅ Add tests for new errors when incorrectly using sa_column * ✅ Add tests for sa_column_args and sa_column_kwargs * ♻️ Do not allow sa_relationship with sa_relationship_args or sa_relationship_kwargs * ✅ Add tests for relationship errors * ✅ Fix test for sa_column_args
		
			
				
	
	
		
			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):
 | |
| 
 | |
|         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):
 | |
| 
 | |
|         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")
 |