What is the difference between
class Base(DeclarativeBase):
TypeError: DeclarativeBase() takes no arguments
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy import Integer, String, ForeignKey, MetaData, create_engine
from typing import List, Optional
engine = create_engine("postgresql://db_user:db_pw@localhost:5432/alembic_learning")
# class Base(DeclarativeBase):
# pass
Base = DeclarativeBase()
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name:Mapped[str] = mapped_column(String(30))
fullname:Mapped[Optional[str]]
addresses:Mapped[List["Address"]] = relationship(back_populates="user")
def __repr__(self) -> str:
class Address(Base):
__tablename__ = "address"
id:Mapped[int] = mapped_column(primary_key=True)
email_address:Mapped[str]
user_id = mapped_column(ForeignKey("
users.id"))
user:Mapped[User] = relationship(back_populates="addresses")
def __repr__(self)-> str:
return f"Address(id={
self.id!r}, email_address={self.email_address})"
Base.metadata.create_all(bind=engine)