Skip to main content

sqlalchemy_seed is simple data seeder using SQLAlchmy.

Project description

https://travis-ci.org/heavenshell/py-sqlalchemy_seed.svg?branch=master Python 3 Updates

sqlalchemy_seed is a seed library which provides initial data to database using SQLAlchemy.

sqlalchemy_seed is similar to Django fixtures.

Installation

pip install sqlalchemy_seed

Adding seed

/myapp
  __init__.py
  models.py
  /fixtures
    accounts.yaml

Model file.

# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
engine = create_engine('sqlite://', convert_unicode=True)

Base = declarative_base()
Base.metadata.bind = engine
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = scoped_session(Session)

class Account(Base):
    __tablename__ = 'accounts'

    id = Column(Integer, primary_key=True)
    first_name = Column(String(100), nullable=False)
    last_name = Column(String(100), nullable=False)
    age = Column(Integer(), nullable=True)

Seed code.

# -*- coding: utf-8 -*-

from sqlalchemy_seed import (
    create_table,
    drop_table,
    load_fixtures,
    load_fixture_files,
)
from myapp.models import Base, session


def main():
    path = '/path/to/fixtures'
    fixtures = load_fixture_files(path, ['accounts.yaml'])
    load_fixtures(session, fixtures)


if __name__ == '__main__':
    main()

Seed file.

- model: myapp.models.Account
  id: 1
  fields:
    first_name: John
    last_name: Lennon
    age: 20

- model: myapp.models.Account
  id: 2
  fields:
    first_name: Paul
    last_name: McCartney
    age: 21

LICENSE

NEW BSD LICENSE.

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_seed-0.3.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_seed-0.3.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sqlalchemy_seed-0.3.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_seed-0.3.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for sqlalchemy_seed-0.3.0.tar.gz
Algorithm Hash digest
SHA256 be89813140fa4fe39d4121d364f9c00090c9509c5ec310bfb9579591fe923a95
MD5 c4bb2bfe9cb88b41e54f625b1ded4a9c
BLAKE2b-256 2fe1ab53ae4d7d9467ee8d17355f4e9f9160662feef68b1fd1dfbf52335224ff

See more details on using hashes here.

File details

Details for the file sqlalchemy_seed-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_seed-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7e241331cfd20a65b79b24c66b4d1a1911a13bfb9aac76f84e60b100c88b1930
MD5 7765e8efb227baff645c420f53a44e5f
BLAKE2b-256 22d2c55b8d416e94b771f0c7b0616292f3340e603e82a1e7af5753d280985c21

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