Simple data seeder using SQLAlchemy.

heavenshell, updated 🕥 2022-08-08 19:02:39

sqlalchemy_seed

.. image:: https://github.com/heavenshell/py-sqlalchemy_seed/workflows/build/badge.svg :target: https://github.com/heavenshell/py-sqlalchemy_seed/actions?query=workflow%3Abuild .. image:: https://pyup.io/repos/github/heavenshell/py-sqlalchemy_seed/python-3-shield.svg :target: https://pyup.io/repos/github/heavenshell/py-sqlalchemy_seed/ :alt: Python 3 .. image:: https://pyup.io/repos/github/heavenshell/py-sqlalchemy_seed/shield.svg :target: https://pyup.io/repos/github/heavenshell/py-sqlalchemy_seed/ :alt: Updates

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

sqlalchemy_seed is similar to Django fixtures <https://docs.djangoproject.com/ja/1.10/howto/initial-data/>_.

Installation

.. code::

pip install sqlalchemy_seed

Adding seed

.. code::

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

Model file.

.. code:: python

# -- 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.

.. code:: python

# -- 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.

.. code::

  • 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

Update seed

If you want idempotent, you can describe seed like followings.

Seed file.

.. code::

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

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

LICENSE

NEW BSD LICENSE.

Issues

Update sqlalchemy to 1.4.40

opened on 2022-08-08 19:02:36 by pyup-bot

This PR updates SQLAlchemy from 1.4.38 to 1.4.40.

The bot wasn't able to find a changelog for this release. Got an idea?

Links - PyPI: https://pypi.org/project/sqlalchemy - Changelog: https://pyup.io/changelogs/sqlalchemy/ - Homepage: https://www.sqlalchemy.org

Update flake8 to 5.0.4

opened on 2022-08-04 10:17:23 by pyup-bot

This PR updates flake8 from 3.9.2 to 5.0.4.

The bot wasn't able to find a changelog for this release. Got an idea?

Links - PyPI: https://pypi.org/project/flake8 - Changelog: https://pyup.io/changelogs/flake8/ - Repo: https://github.com/pycqa/flake8

Update pyyaml to 6.0

opened on 2022-06-24 14:21:23 by pyup-bot

This PR updates pyyaml from 5.4.1 to 6.0.

The bot wasn't able to find a changelog for this release. Got an idea?

Links - PyPI: https://pypi.org/project/pyyaml - Homepage: https://pyyaml.org/

Feature/add table support

opened on 2019-06-23 15:29:57 by scaryclam

This adds some support for creating sqlalchemy Table object inserts.

This is mainly to aid in the support of M2M relations in projects that use Table rather than Model.

The PR should be considered a WIP for now as I'd like some feedback on whether the approach is acceptable.

Releases

0.3.0 2019-05-08 11:50:08

  • Bump 0.3.0 #56
  • Use Merge to Prevent Duplicated Data #54 (@crcornwell)
  • Fix typo in SeedMixin.tearDownClass name #53 (@rileyjohngibbs)

0.2.0 2019-05-04 05:17:02

  • Bump 0.2.0 #52
  • specify the loader in yaml.load call #51 (@crcornwell)
Shinya Ohyanagi

Vim, Python, TypeScript and React

GitHub Repository

python sqlalchemy