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 (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
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20e6a77d773270265ffd226b73a8c8e5379c4198a85aaed91f253c716c71a7b9 |
|
MD5 | af46aa87f4fe03b010a4ee3bd99ade6c |
|
BLAKE2b-256 | 3a5b7b90abde31eef9a2fabc7b49489f03e82b061cc0651b28ce081adff88a00 |
File details
Details for the file sqlalchemy_model_builder-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_model_builder-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a1 CPython/3.8.10 Linux/5.8.0-1033-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a66ec2dd16caec43bb90765aaa05db9de7c01427c5082d0bfaa36e35d37381c2 |
|
MD5 | 6822e0b20ef46af6ee75183f182c5f55 |
|
BLAKE2b-256 | 9ebcd28c2aec1fcf2358d6b1898d18b053e33ed3800413c94da509e2083ad7fc |