{ "info": { "author": "Nathan/Eilisha Shiraini", "author_email": "neshiraini+sqlalchemy@heptacle.fr", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3", "Topic :: Database" ], "description": "SqlAlchemy Enumeration Tables\n=============================\n\nSQLAlchemy has built-in ``enum.Enum`` support,\nvia its column type ``sqlalchemy.Enum``.\nHowever, this type relies either on the backend's enum type,\nor on a check constraints. Both of these are immutable objects,\nwhich are a pain in the butt to modify\n(only PostgreSQL supports adding values to an enum type,\nand even then it doesn't support removing them).\n\nAnother often-used pattern to support enums in a database\nis via a dedicated table that reflects the enum values.\nThis requires updating the table everytime the enum is modified,\nbut doing so is much simpler than replacing a type.\n\nThis package allows you to create the enum table,\nand columns referencing that table, directly from\na Python enum class. It also interfaces with Alembic\nto automatically add ``INSERT`` and ``DELETE`` statements\nto your autogenerated migration scripts.\n\nWhen to use\n-----------\n\n1. Only works with Pythons's enumeration classes,\n or at least one with a behavior similar to ``enum.Enum``.\n Does not work with collections of arbitrary entries.\n2. Only works with SqlAlchemy's declarative ORM system.\n If you only use SqlAlchemy Core... *deal with it*.\n3. Better used for frequently updated enumeration classes.\n4. **Do not** use with another package that provides\n ``op.enum_insert`` and ``op.enum_delete`` operations in Alembic.\n\nHow to use with SqlAlchemy\n--------------------------\n\n::\n\n import enum\n import sqlalchemy as sa\n from sqlalchemy.ext.declarative import declarative_base\n\n import enumtables as et\n\n # Create the Python enumeration class\n class MyEnum(enum.Enum):\n HELLO = \"HELLO\"\n WORLD = \"WORLD\"\n\n Base = declarative_base()\n\n # Create the enumeration table\n # Pass your enum class and the SQLAlchemy declarative base to enumtables.EnumTable\n MyEnumTable = et.EnumTable(MyEnum, Base)\n\n # Create a model class that uses the enum\n class MyModel(Base):\n __tablename__ = \"my_model\"\n # Pass the enum table (not the enum class) to enumtables.EnumColumn\n # It replaces sqlalchemy.Column, but aside from the enum table,\n # it can take the same parameters.\n # It will automatically create a ForeignKeyConstraint referencing the enum table.\n enum_value = et.EnumColumn(MyEnumTable, primary_key = True)\n\n # When valued (on an instance of MyModel), enum_value will be an instance of MyEnum.\n\nFirst, the ``EnumTable`` factory takes the enum class and the declarative base class\nto create the actual ORM class. Then this ORM class is passed to the ``EnumColumn`` class\nto create the column linked to the enum table.\nThe column behaves just as if it had SqlAlchemy's own ``Enum`` type.\n\nOn the implementation side, ``EnumTable`` is not a class,\nit's a factory function that performs Python black magic\nto create a subclass of the declarative base, and set it up to be a DB table\ncontaining the enum items (actually it just has one column ``item_id`` of type String).\n\n``EnumColumn`` is a subclass of SqlAlchemy's ``Column`` that gets initialized\nwith a custom type and a foreign key to the enum table.\n\nHow to use with Alembic\n-----------------------\n\nFirst add::\n\n import enumtables\n\nat the begining of your ``env.py`` file,\nthen add the same line in the imports of your ``script.py.mako`` file.\nThe package uses Alembic's standard hooks to take care of migration generation.\n\nDon't forget to review the migrations afterwards.\nEspecially make sure that, if the table did not exist before,\nthe ``op.enum_insert`` commands are located *after* the corresponding ``op.create_table`` command.\n\nOther uses\n-----------\n\nUsing the enum table class directly\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nThe enum table class behaves like any SqlAlchemy ORM class::\n\n enum_query = session.query(MyEnumTable)\n result = enum_query.first()\n\n # The column item_id stores the name of the enum item as a string\n enum_name = result.item_id\n\nAdding more columns to the enum tables\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nAny keyword argument passed to the ``EnumTable`` factory becomes a member of the table class.\nWhich means, you can pass anything (like a column) exactly as you would defined a usual ORM class::\n\n BetterEnumTable = et.EnumTable(\n MyEnum,\n\t\tBase,\n\n # tablename is turned into __tablename__\n tablename = \"better_enum\",\n\n # Let's add a new column!\n order = sa.Column(sa.Integer, nullable = False),\n\n # And since it's an ordering number, let's make it unique too.\n __table_args__ = (\n sa.UniqueConstraint('order'),\n ),\n )\n\n", "description_content_type": "text/x-rst", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://git.heptacle.fr/neshiraini/sqlalchemy-enum-tables", "keywords": "sql sqlalchemy orm enum alembic migrations database relational", "license": "Apache Software License", "maintainer": "", "maintainer_email": "", "name": "SqlAlchemy-Enum-Tables", "package_url": "https://pypi.org/project/SqlAlchemy-Enum-Tables/", "platform": "", "project_url": "https://pypi.org/project/SqlAlchemy-Enum-Tables/", "project_urls": { "Homepage": "https://git.heptacle.fr/neshiraini/sqlalchemy-enum-tables" }, "release_url": "https://pypi.org/project/SqlAlchemy-Enum-Tables/1.1.0/", "requires_dist": [ "sqlalchemy", "alembic ; extra == 'alembic'" ], "requires_python": "", "summary": "Making Python enums into SQLAlchemy tables with support for Alembic migrations", "version": "1.1.0" }, "last_serial": 5517363, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "ff8930b0af7a3a056a318b61f31ae10e", "sha256": "534ea41df17c8d0cedde3118b24f07b032ecf63ac43fa04610aa92cbf1e7470d" }, "downloads": -1, "filename": "SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "ff8930b0af7a3a056a318b61f31ae10e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7080, "upload_time": "2019-05-28T18:54:28", "url": "https://files.pythonhosted.org/packages/7a/ae/52c8f34dbe81e269eea451d80d2a43f5dadc08676c535b3b361e2d1fc4fd/SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "a0ced16a617fa58b05c7ee688e025bff", "sha256": "4e732b43292272a310038eb47e663437aa161ea7635f50ae3e6f745f24ef9012" }, "downloads": -1, "filename": "SqlAlchemy Enum Tables-1.0.0.tar.gz", "has_sig": false, "md5_digest": "a0ced16a617fa58b05c7ee688e025bff", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6245, "upload_time": "2019-05-28T18:54:30", "url": "https://files.pythonhosted.org/packages/1d/74/2b1e17626aa0977e4bfbb8874029a8b20051f65579c87bef9b614fd0e141/SqlAlchemy%20Enum%20Tables-1.0.0.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "fb265f962cdbb53586be88c0142a2fe9", "sha256": "714edc93ed9d2614177e822db7963daccb47b4273224ce709c4579c400186cc5" }, "downloads": -1, "filename": "SqlAlchemy_Enum_Tables-1.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "fb265f962cdbb53586be88c0142a2fe9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7096, "upload_time": "2019-07-11T12:25:11", "url": "https://files.pythonhosted.org/packages/08/62/2cd04b67fddd36d0bedcbba5c386f2c93334ad3088779bd18426045c1ae6/SqlAlchemy_Enum_Tables-1.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "2e165b5d9a34bea689b897297e8a2ac6", "sha256": "e788b580c11fecb214c17669f94da597b2043fc8baaee35c9f4af8f82bb6d22f" }, "downloads": -1, "filename": "SqlAlchemy Enum Tables-1.1.0.tar.gz", "has_sig": false, "md5_digest": "2e165b5d9a34bea689b897297e8a2ac6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6268, "upload_time": "2019-07-11T12:25:12", "url": "https://files.pythonhosted.org/packages/db/20/5f7e70de7726490bdcb0008bec20d5763ffd5981fdeedb9dfc0d819dfedc/SqlAlchemy%20Enum%20Tables-1.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fb265f962cdbb53586be88c0142a2fe9", "sha256": "714edc93ed9d2614177e822db7963daccb47b4273224ce709c4579c400186cc5" }, "downloads": -1, "filename": "SqlAlchemy_Enum_Tables-1.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "fb265f962cdbb53586be88c0142a2fe9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7096, "upload_time": "2019-07-11T12:25:11", "url": "https://files.pythonhosted.org/packages/08/62/2cd04b67fddd36d0bedcbba5c386f2c93334ad3088779bd18426045c1ae6/SqlAlchemy_Enum_Tables-1.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "2e165b5d9a34bea689b897297e8a2ac6", "sha256": "e788b580c11fecb214c17669f94da597b2043fc8baaee35c9f4af8f82bb6d22f" }, "downloads": -1, "filename": "SqlAlchemy Enum Tables-1.1.0.tar.gz", "has_sig": false, "md5_digest": "2e165b5d9a34bea689b897297e8a2ac6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6268, "upload_time": "2019-07-11T12:25:12", "url": "https://files.pythonhosted.org/packages/db/20/5f7e70de7726490bdcb0008bec20d5763ffd5981fdeedb9dfc0d819dfedc/SqlAlchemy%20Enum%20Tables-1.1.0.tar.gz" } ] }