mirror of
				https://github.com/fastapi/sqlmodel.git
				synced 2025-10-31 18:15:44 +08:00 
			
		
		
		
	 d8effcbc5c
			
		
	
	d8effcbc5c
	
	
	
		
			
			* 📝 Add source examples for Python 3.9 and 3.10 * ✅ Add tests for new source examples for Python 3.9 and 3.10, still needs pytest markers * ✅ Add tests for fastapi examples * ✅ Update tests for FastAPI app testing, for Python 3.9 and 3.10, fixing multi-app testing conflicts * ✅ Require Python 3.9 and 3.10 for tests * ✅ Update tests with missing markers
		
			
				
	
	
		
			42 lines
		
	
	
		
			983 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			983 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| from sqlmodel import Field, Session, SQLModel, create_engine
 | |
| 
 | |
| 
 | |
| class Hero(SQLModel, table=True):
 | |
|     id: int | None = Field(default=None, primary_key=True)
 | |
|     name: str
 | |
|     secret_name: str
 | |
|     age: int | None = None
 | |
| 
 | |
| 
 | |
| sqlite_file_name = "database.db"
 | |
| sqlite_url = f"sqlite:///{sqlite_file_name}"
 | |
| 
 | |
| engine = create_engine(sqlite_url, echo=True)
 | |
| 
 | |
| 
 | |
| def create_db_and_tables():
 | |
|     SQLModel.metadata.create_all(engine)
 | |
| 
 | |
| 
 | |
| def create_heroes():  # (1)!
 | |
|     hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")  # (2)!
 | |
|     hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
 | |
|     hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
 | |
| 
 | |
|     with Session(engine) as session:  # (3)!
 | |
|         session.add(hero_1)  # (4)!
 | |
|         session.add(hero_2)
 | |
|         session.add(hero_3)
 | |
| 
 | |
|         session.commit()  # (5)!
 | |
|     # (6)!
 | |
| 
 | |
| 
 | |
| def main():  # (7)!
 | |
|     create_db_and_tables()  # (8)!
 | |
|     create_heroes()  # (9)!
 | |
| 
 | |
| 
 | |
| if __name__ == "__main__":  # (10)!
 | |
|     main()  # (11)!
 |