id, username, email, password, products라는 속성을 가지고 있음
여기서 products란 Product모델과의 연결을 위해 relationship을 통해 product와 연결한 것을 의미함
class Seller(Base):
__tablename__='sellers'
id = Column(Integer, primary_key=True, index=True)
username = Column(String)
email = Column(String)
password = Column(String)
products=relationship('Product',back_populates='seller')
product 객체에 대해서 post를 통해 데이터베이스에 엔트리를 생성할 때 seller의 id도 매개변수로 넣어 해당 product를 판매하는 seller를 알려주고 다른 seller들과 식별할 수 있도록 함
class Product(Base): #데이터베이스 테이블을 만들기 위해 작성
__tablename__='products'
id= Column(Integer,primary_key=True,index=True)
name=Column(String)
description=Column(String)
price=Column(Integer)
seller_id=Column(Integer,ForeignKey('sellers.id'))
seller=relationship("Seller",back_populates='products')
@router.post('/',status_code=status.HTTP_201_CREATED)
def add(request:schemas.Product,db:Session =Depends(get_db)):
new_product=models.Product(name=request.name,description=request.description,price=request.price, seller_id=1)
db.add(new_product)
db.commit()
db.refresh(new_product)
return request