File size: 1,028 Bytes
db7c1e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Base class for SQLAlchemy models
"""
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import Column, DateTime, func
from sqlalchemy.ext.asyncio import AsyncAttrs
from datetime import datetime
import uuid
from sqlalchemy.dialects.postgresql import UUID


class Base(AsyncAttrs, DeclarativeBase):
    """
    Base class for all SQLAlchemy models
    Includes common columns and configurations
    """
    __abstract__ = True

    # Common columns for all models
    created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False)

    def __init__(self, *args, **kwargs):
        # Set the id automatically if not provided
        if 'id' not in kwargs and hasattr(self, 'id') and self.id is None:
            # For models that have an id column, set a default UUID if not provided
            pass  # The column default will handle this
        super().__init__(*args, **kwargs)