Hi,

I am looking to retrieving the pure python class that participates in a 
mapping. I would like to do this **after** the mapping took place and 
modified/extended the class' attributes.

For instance, with this code
class Thing(object):
  def __init__(self, id=None, name=None):
    self.id = id
    self.name = name

  def do_something(): pass

things = Table(
  "things",
  metadata,
  Column("id", Integer, primary_key=True),
  Column("name", String)
)

mapper(Thing, things)

After the mapper is called, it looks like the original class is modified 
beyond repair. By this I mean it is no longer possible to consider it a 
pure class that can be used for OO tasks, such as inheriting from it to 
inherit behavior. Instead, a table is now directly associated with it and 
inheriting from it will either cause problems or create a 2nd table, and 
will bring all the sql alchemy baggage with it (validators, relationships, 
etc.)

I would like to access the pure 'Thing" class as it stands before the call 
to mapper(). The use-case is basically that I want to inherit behavior of 
the ORM class, but behavior only. I possibly want to map the inherited 
subclass to a *different* table.

I know that this can be done  this way, which also describes my use-case
class _Thing(object):
  id = Column(Integer, primary_key=True)
  name = Column(String)
  def do_something(): pass

class Thing(_Thing, Base):
  __tablename__ = "things"

class ThingCopy(_Thing, Base):
  __tablename__ = "thing_copies"
  # twist!
  original_thing_id = Column(Integer, ForeignKey(Thing.id))
  original_thing = relationship(Thing)
  

So I have "copies" of my original items that reside in different tables, 
but they 1) share all behavior and 2) share a copy of *almost* all sql 
alchemy descriptors/behavior. Now the example above may look cute, but in 
reality if we are talking large classes with relationships, the 
@declared_attrs that have to go into the superclass there quickly turn the 
code into an atrocity. In addition, I want to add these "copies" to all my 
existing code without 1) having to refactor all if it and 2) having to 
teach all co-workers how not to do things the declarative base way.

Ideally, something like this
class Thing(Base):
  #regular declarative business...
  __tablename__ = "things"
  id = Column(Integer, primary_key=True)
  name = Column(String)
  def do_something(): pass


#later, I extend the code with this unintrusive addition
class ThingCopy(Thing._pure_class): #you get the idea
  __tablename__ = "thing_copies"
  original_thing_id = Column(Integer, ForeignKey(Thing.id))
  original_thing = relationship(Thing)


Having to split all my ORM classes into "behavior" and "mapping" logic 
results in fragmented code, when it looks like it should belongs together. 
I've also seen this recipe 
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName, which 
I think is even worse in that regard.

Could this be supported?

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to