I'm working on a SQLAlchemy <http://www.sqlalchemy.org/> defining a bunch 
of mixin classes 
<http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html> 
that applications should be able to import and extend their model.

When looking at the documentation 
<http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html#mixing-in-relationships>,
  
mixin classes are create knowing the final table name however, in the case 
of a generic library, the final table name that will be used by the 
application is not known.

Take the following mixin classes:

import sqlalchemy as sa

class UserMixin(object):
    id = sa.Column(sa.Integer(), primary_key=True)
    first_name = sa.Column(sa.Unicode(255))
    last_name  = sa.Column(sa.Unicode(255))

class ItemMixin(object):
    id = sa.Column(sa.Integer(), primary_key=True)
    name = sa.Column(sa.Unicode(255))
    short_description = sa.Column(sa.Unicode(255))

class OrdersMixin(object):
    id = sa.Column(sa.Integer(), primary_key=True)
    user_id = sa.Column(sa.Integer(), sa.ForeignKey('???'))
    item_id = sa.Column(sa.Integer(), sa.ForeignKey('???'))

Then an application defining its models:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
    
Base = declarative_base()

class MyUser(UserMixin, Base):
    __tablename__ = 'myuser'

class MyItem(ItemMixin, Base):
    __tablename__ = 'myitem'
    total = sa.Column(sa.Integer())

class MyOrders(OrdersMixin, Base):
    __tablename__ = 'myorders'

I have two issues with this model:


   1. Except from redefining the relationship columns in the extending 
   models, how can the mixin class build the relationship on its own.
   2. Type of the foreign key is assumed by the mixin class, but the `id` 
   of the table may come from the application itself or from another mixin 
   class.
   
Is the model I'm trying to implement correct? What would be the right way 
to tackle this problem?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to