SQLAlchemy Model Builder
Project description
SQLAlchemy Model Builder
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
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 768a3c7a908955a757f54d868fdda53b5ce991ce9fcab3eb7684e620f5e09c05 |
|
MD5 | 2c5c62dc0cd88fd8c9080ba1d6284c9a |
|
BLAKE2b-256 | 662748668d8ecd0830f8600fc256c47eaf90e3415b78976daa5f865a90766868 |
File details
Details for the file sqlalchemy_model_builder-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_model_builder-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a2 CPython/3.8.11 Linux/5.8.0-1039-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4d833663f10d3d42a8f5d8e44290ef242b778c635af4fc002231018ea57809c |
|
MD5 | 99096b1e97330c490e38bc76c92fd10b |
|
BLAKE2b-256 | 9a2bd676a0f16066478e34e9482ed98f0b099e13354683dc4f9de1ef1c9b36fa |