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
  • Float
  • Integer
  • Interval
  • LargeBinary
  • MatchType (Todo)
  • Numeric
  • 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.6.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: sqlalchemy-model-builder-0.0.6.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.8.11 Linux/5.8.0-1039-azure

File hashes

Hashes for sqlalchemy-model-builder-0.0.6.tar.gz
Algorithm Hash digest
SHA256 768a3c7a908955a757f54d868fdda53b5ce991ce9fcab3eb7684e620f5e09c05
MD5 2c5c62dc0cd88fd8c9080ba1d6284c9a
BLAKE2b-256 662748668d8ecd0830f8600fc256c47eaf90e3415b78976daa5f865a90766868

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlalchemy_model_builder-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d4d833663f10d3d42a8f5d8e44290ef242b778c635af4fc002231018ea57809c
MD5 99096b1e97330c490e38bc76c92fd10b
BLAKE2b-256 9a2bd676a0f16066478e34e9482ed98f0b099e13354683dc4f9de1ef1c9b36fa

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