Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
922 B
3
Indexable
Never
from sqlalchemy import Table, Column, inspect
from .base_model import BaseModel, metadata
from app.config.database import engine
from strawberry import type

inspector = inspect(engine)
table_name = "mdm_product_view"

if table_name not in inspector.get_table_names():
    raise Exception(f"Table '{table_name}' not found in the database")

table = Table(table_name, metadata, autoload_with=engine)

column_names = table.columns.keys()
primary_keys = [key.name for key in table.primary_key.columns]

if not primary_keys:
    primary_keys = ["RECORD_ID"]

attributes = {"__tablename__": table_name}

for column in table.columns:
    column_type = column.type

    if column.name in primary_keys:
        atrributes[column.name] = Column(column_type, primary_key=True)
    else:
        attributes[column.name] = Column(column_type)

DynamicModel = type("DynamicModel", (BaseModel,), attributes)
Leave a Comment