Created
September 5, 2023 11:04
-
-
Save mansha99/5f038932f39b6539be2d4fc6a44a8e1a to your computer and use it in GitHub Desktop.
using DeclarativeBase to define Models of ORM in SQLAlchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import String | |
from sqlalchemy.orm import DeclarativeBase | |
from sqlalchemy import Integer,String,Float | |
from sqlalchemy.orm import Mapped | |
from sqlalchemy.orm import mapped_column | |
class Base(DeclarativeBase): | |
pass | |
class Planet(Base): | |
__tablename__ = "planet" | |
id: Mapped[int] = mapped_column(Integer,primary_key=True) | |
name: Mapped[str] = mapped_column(String(32)) | |
diameter: Mapped[float] = mapped_column(Float) | |
def __repr__(self) -> str: | |
return f"Planet [ Id: {self.id}, Name: {self.name}, Diameter: {self.diameter} ]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment