mirror of
				https://github.com/fastapi/sqlmodel.git
				synced 2025-11-04 14:47:08 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			30 lines
		
	
	
		
			669 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			669 B
		
	
	
	
		
			Python
		
	
	
	
	
	
from sqlmodel import Session
 | 
						||
 | 
						||
from .database import create_db_and_tables, engine
 | 
						||
from .models import Hero, Team
 | 
						||
 | 
						||
 | 
						||
def create_heroes():
 | 
						||
    with Session(engine) as session:
 | 
						||
        team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
 | 
						||
 | 
						||
        hero_deadpond = Hero(
 | 
						||
            name="Deadpond", secret_name="Dive Wilson", team=team_z_force
 | 
						||
        )
 | 
						||
        session.add(hero_deadpond)
 | 
						||
        session.commit()
 | 
						||
 | 
						||
        session.refresh(hero_deadpond)
 | 
						||
 | 
						||
        print("Created hero:", hero_deadpond)
 | 
						||
        print("Hero's team:", hero_deadpond.team)
 | 
						||
 | 
						||
 | 
						||
def main():
 | 
						||
    create_db_and_tables()
 | 
						||
    create_heroes()
 | 
						||
 | 
						||
 | 
						||
if __name__ == "__main__":
 | 
						||
    main()
 |