Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with alembic #91

Open
abnerjacobsen opened this issue Apr 12, 2023 · 0 comments
Open

Help with alembic #91

abnerjacobsen opened this issue Apr 12, 2023 · 0 comments

Comments

@abnerjacobsen
Copy link

Hello,

I'm replacing all the database access implementations I used previously with Fatsapi-sqla, and now I need to integrate it with alembic.

What would be the best way to use Fastapi-sqla in alembic?

Do you have any examples of a standard implementation?

I'm defining my models as follows:

from datetime import datetime
from fastapi_sqla import Base
from sqlalchemy.dialects.postgresql.json import JSONB
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.sql import expression
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    UniqueConstraint,
    PrimaryKeyConstraint,
    DateTime,
    text,
)
import cuid

class EntityModel(Base):
    __tablename__ = "entities"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="entities_pkey"),
        UniqueConstraint("business_id", name="entities_business_id_ukey"),
    )

    id = Column(String(25), default=cuid.cuid, nullable=False, primary_key=True)
    owner = Column(String(25), nullable=False)
    name = Column(String(128), nullable=False)
    email = Column(String(255), nullable=False)
    active = Column(
        Boolean, nullable=False, default=False, server_default=expression.false()
    )
    status = Column(Integer, nullable=False, default=0, server_default=text("0"))
    country = Column(String(3), default="BRA", nullable=False)
    business_id = Column(String(25), nullable=False)
    external_id = Column(String(128), nullable=True)
    partner_id = Column(String(25), nullable=True)
    profile = Column(MutableDict.as_mutable(JSONB))
    created_at = Column(
        DateTime(timezone=False), default=datetime.utcnow, nullable=False
    )
    updated_at = Column(
        DateTime(timezone=False),
        default=datetime.utcnow,
        onupdate=datetime.utcnow,
        nullable=False,
    )

    def __init__(self, *args, **kwargs):
        if "id" not in kwargs:
            kwargs["id"] = cuid.cuid()
        super(EntityModel, self).__init__(*args, **kwargs)

Is this the best way to do it ?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant