10

I have some standard SQLAlchemy models that I reuse across projects. Something like this:

from sqlalchemy import Column, Integer, String, Unicode
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Category(Base):
    __tablename__ = 'category'

    id = Column(Integer, primary_key=True)
    slug = Column(String(250), nullable=False, unique=True)
    title = Column(Unicode(250), nullable=False)

    def __call__(self):
        return self.title

I'd like to put this in a shared library and import it into each new project instead of cutting and pasting it, but I can't, because the declarative_base instance is defined separately in the project. If there's more than one, they won't share sessions. How do I work around this?

Here's another question that suggests using mixin classes. Could that work? Will SQLAlchemy accurately import foreign keys from mixin classes?

Community
  • 1
  • 1
Kiran Jonnalagadda
  • 2,606
  • 1
  • 30
  • 29
  • What are you doing with the Base objects after this? Last I remember, I was able to merge together multiple metadata's from multiple Base objects by binding them all to the same engine. I would imagine that it would be enough to use a session from that engine to access all of the tables you've defined. – Mark Hildreth Apr 27 '11 at 18:57
  • Nothing further with the Base object. If binding to metadata is all it takes, I could easily import and rebind -- while ensuring that two separate apps don't run in the same Python process. – Kiran Jonnalagadda Jun 03 '11 at 14:11

1 Answers1

5

When you call

Base = declarative_base()

SA create new metadata for this Base.

To reuse your models you must bind metadata of main models to reusable models, but before any import of your reusable models by:

Base.metadata = my_main_app.db.metadata

MixIn classes useful for repeating column declarations, and extending class methods. For connecting reusable apps based on MixIns you must define concrete class in code manualy for each model.

Will SQLAlchemy accurately import foreign keys from mixin classes?

MixIn class with foreign key and constraint

from sqlalchemy.schema import UniqueConstraint
from sqlalchemy.ext.declarative import declared_attr

class MessageMixIn(object):
    ttime = Column(DateTime)

    @declared_attr
    def sometable_id(cls):
        return Column(Integer, ForeignKey('sometable.id'))

    @declared_attr
    def __table_args__(cls):
        return (UniqueConstraint('sometable_id', 'ttime'), {})
estin
  • 3,051
  • 1
  • 24
  • 31