Skip to main content

SQLAlchemy Model Builder

Project description

SQLAlchemy Model Builder

test publish codecov pypi

Features

  • Build and Save SQLALchemy models with random data
  • Build relationships
  • Build minimal (with required) fields only

How to use

Build SQLAlchemy model:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.sqltypes import Integer, String, Text

from sqlalchemy_model_builder import ModelBuilder

Base = declarative_base()


class Address(Base):
    __tablename__ = "addresses"

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    user = relationship("User", back_populates="addresses")


class User(Base):
    __tablename__ = "users"

    addresses = relationship("Address", back_populates="user")
    bio = Column(Text)
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)


random_user = ModelBuilder(User).build()  # This will not insert the User

minimal_random_user = ModelBuilder(User, minimal=True).build()  # Builds User with `id` and `name`

random_address = ModelBuilder(Address).build(user_id=user.id)  # Build with `user_id`

Save SQLAlchemy model:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.sqltypes import Integer, String

from sqlalchemy_model_builder import ModelBuilder

Base = declarative_base()

engine = create_engine("sqlite://", echo=True)


class Address(Base):
    __tablename__ = "addresses"

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    user = relationship("User", back_populates="addresses")


class User(Base):
    __tablename__ = "users"

    addresses = relationship("Address", back_populates="user")
    bio = Column(Text)
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)


Base.metadata.create_all(engine)

LocalSession = sessionmaker(bind=engine)

db = LocalSession()


random_user = ModelBuilder(User).save(db=db)  # Builds and Saves model using provided session

random_address = ModelBuilder(Address).save(db=db, user_id=user.id)  # Save with `user_id`

Supported Data Types

  • BigInteger
  • Boolean
  • Date
  • DateTime
  • Enum (Todo)
  • Float
  • Integer
  • Interval
  • LargeBinary (Todo)
  • MatchType (Todo)
  • Numeric (Todo)
  • PickleType (Todo)
  • SchemaType (Todo)
  • SmallInteger
  • String
  • Text
  • Time
  • Unicode
  • UnicodeText

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy-model-builder-0.0.5.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_model_builder-0.0.5-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-model-builder-0.0.5.tar.gz.

File metadata

  • Download URL: sqlalchemy-model-builder-0.0.5.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a1 CPython/3.8.10 Linux/5.8.0-1033-azure

File hashes

Hashes for sqlalchemy-model-builder-0.0.5.tar.gz
Algorithm Hash digest
SHA256 20e6a77d773270265ffd226b73a8c8e5379c4198a85aaed91f253c716c71a7b9
MD5 af46aa87f4fe03b010a4ee3bd99ade6c
BLAKE2b-256 3a5b7b90abde31eef9a2fabc7b49489f03e82b061cc0651b28ce081adff88a00

See more details on using hashes here.

File details

Details for the file sqlalchemy_model_builder-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_model_builder-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a66ec2dd16caec43bb90765aaa05db9de7c01427c5082d0bfaa36e35d37381c2
MD5 6822e0b20ef46af6ee75183f182c5f55
BLAKE2b-256 9ebcd28c2aec1fcf2358d6b1898d18b053e33ed3800413c94da509e2083ad7fc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page