how to pass metadata from few files with tables to here class PostgresqlManager: def…
- User
how to pass metadata from few files with tables to here class PostgresqlManager: def init(self): self.engine = create_async_engine(settings.database_url()) self.metadata = Base.metadata self.Session = async_sessionmaker[AsyncSession](self.engine, expire_on_commit=False)
async def create_tables(self): async with self.engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) print(Base.metadata.tables.keys()) - Assistant
One way to pass metadata from files with tables to the PostgresqlManager class is to define the metadata in each file and then import it into the PostgresqlManager class. For example, in the file with the table definitions:
from sqlalchemy import Column, Integer, String, Table, MetaData metadata = MetaData() my_table = Table( 'my_table', metadata, Column('id', Integer, primary_key=True), Column('name', String), ... )Then, in the PostgresqlManager class:
from my_table_file import metadata class PostgresqlManager: def __init__(self): self.engine = create_async_engine(settings.database_url()) self.metadata = metadata self.Session = async_sessionmaker[AsyncSession](self.engine, expire_on_commit=False) async def create_tables(self): async with self.engine.begin() as conn: await conn.run_sync(self.metadata.create_all) print(self.metadata.tables.keys())This approach allows you to define the tables with their metadata in separate files and then import them into the class that will manage the database connection.
- User
but I have files with similar tables like this import uuid from datetime import date
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.dialects.postgresql import VARCHAR, DATE
from src.db.tables.base import Base from src.db.tables.customer_object import CustomerObject
class Customer(Base): tablename = 'customer'
name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) noservice_date: Mapped[date | None] = mapped_column(DATE, default=None) customer_objs: Mapped[list["CustomerObject"]] = relationship('CustomerObject', secondary='customer_customer_obj', back_populates='customers', lazy='subquery')class CustomerCustomerObject(Base): tablename = 'customer_customer_obj'
customer_id: Mapped[uuid.UUID] = mapped_column(ForeignKey('customer.id'), nullable=False) customer_obj_id: Mapped[uuid.UUID] = mapped_column(ForeignKey('customer_obj.id'), nullable=False) customer: Mapped["Customer"] = relationship('Customer', back_populates='customer_objs', lazy='subquery') customer_obj: Mapped["CustomerObject"] = relationship('CustomerObject', back_populates='customers', lazy='subquery') - Assistant
- User
I didn't get it. I have two files - one with class Customer(Base): tablename = 'customer'
name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) noservice_date: Mapped[date | None] = mapped_column(DATE, default=None) customer_objs: Mapped[list["CustomerObject"]] = relationship('CustomerObject', secondary='customer_customer_obj', back_populates='customers', lazy='subquery')and another with class OurDoc(Base): tablename = 'our_doc'
doc_name: Mapped[str] = mapped_column(VARCHAR(32), unique=True, nullable=False) added_at: Mapped[datetime] = mapped_column(TIMESTAMP, nullable=False, server_default=func.now()) novalid_date: Mapped[datetime | None] = mapped_column(TIMESTAMP, default=None) system_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("help_os_system.id"), nullable=False) system: Mapped["HelpOsSystem"] = relationship(lazy='subquery') and how to get metadata from both - Assistant
- User
maybe there is easier way? maybe somehow we can use Base
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.