/o1mini Поправь связи в БД, мне приходится  везде  использовать lazy='selectin', чтобы не было ошибок, но они все равно появляются. Может это из-за UniqueConstraint, я обычно ее не использовал в БД. sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_on ly() here. Was IO attempted in an unexpected place? (Background on this error at: https ://sqlalche.me/e/20/xd2s from datetime import datetime
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, relationship, declarative_base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, BigInteger, UniqueConstraint
import asyncio
DATABASE_URL = "postgresql+asyncpg://postgres:Giga.Giga1@localhost/fami"

Base = declarative_base()

class User(Base):
    tablename = 'sdxl_users'
    id = Column(Integer, primary_key=True, index=True)
    firstname = Column(String, nullable=True)
    lastname = Column(String, nullable=True)
    username = Column(String, nullable=True)
    telegram_id = Column(BigInteger, nullable=False, unique=True)
    date_joined = Column(DateTime, default=datetime.utcnow, nullable=False)
    utc_offset = Column(Integer, nullable=True)  # UTC смещение в часах

    prompts = relationship("Prompt", back_populates="user",lazy='selectin')
    reminders = relationship("Reminder", back_populates="user",lazy='selectin')
    user_tokens = relationship("UserToken", back_populates="user",lazy='selectin')
class Credential(Base):
    tablename = 'sdxl_felo_credentials'
    id = Column(Integer, primary_key=True, index=True)
    login = Column(String, unique=True, nullable=False)
    password = Column(String, nullable=False)  # Хранить в открытом виде
    token = Column(String, unique=True, nullable=True)
    expires_at = Column(DateTime(timezone=True), nullable=True)
    reset_date = Column(DateTime(timezone=True), nullable=True)  # Дата последнего сброса
    usage_count = Column(Integer, default=0, nullable=False)

    user_tokens = relationship("UserToken", back_populates="credential",lazy="selectin")

class UserToken(Base):
    tablename = 'sdxl_felo_user_tokens'
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('sdxl_users.id', ondelete='CASCADE'), nullable=False)
    credential_id = Column(Integer, ForeignKey('sdxl_felo_credentials.id', ondelete='CASCADE'), nullable=False)

    user = relationship("User", back_populates="user_tokens",lazy="selectin")
    credential = relationship("Credential", back_populates="user_tokens", lazy="selectin")

    table_args = (UniqueConstraint('user_id', 'credential_id', name='_user_token_uc'),)

class Prompt(Base):
    tablename = 'sdxl_prompts'
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('sdxl_users.id'), nullable=False)
    prompt_text = Column(String, nullable=False)

    user = relationship("User", back_populates="prompts",lazy='selectin')

class Reminder(Base):
    tablename = 'sdxl_reminders'
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('sdxl_users.id'), nullable=False)
    reminder_text = Column(String, nullable=False)
    remind_at = Column(DateTime(timezone=True), nullable=False)
    end_at = Column(DateTime(timezone=True), nullable=True)
    chat_id = Column(BigInteger, nullable=False)
    thread_id = Column(Integer, nullable=True)
    cron = Column(String, nullable=True)
    user = relationship("User", back_populates="reminders",lazy='selectin')

engine = create_async_engine(DATABASE_URL, echo=False)
SessionLocal = sessionmaker(autocommit=False,expire_on_commit=False, autoflush=False, bind=engine, class_=AsyncSession)

async def init_db():
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
if name == 'main':
    asyncio.run(init_db())