[sqlalchemy] Re: Abstract base class

2008-12-19 Thread FrankB

Hi,

I have a similar task, so I tried to use your proposal, but it didn't
work for me:

===

from sqlalchemy import types as satypes
from sqlalchemy import schema as saschema
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base,
DeclarativeMeta
from sqlalchemy.orm import scoped_session, sessionmaker

class BaseType(DeclarativeMeta):
def __init__(newcls, classname, bases, dict_):
newcls.notes = saschema.Column(satypes.String)
DeclarativeMeta.__init__(newcls, classname, bases, dict_)

MetaData = saschema.MetaData(bind=create_engine('sqlite:///:memory:'))
Session = scoped_session(sessionmaker(bind=MetaData.bind))
Base = declarative_base(metadata=MetaData, mapper=Session.mapper,
metaclass=BaseType)

class MasterEntity(Base):
__tablename__ = master
id = saschema.Column(satypes.Integer, primary_key=True)
status = saschema.Column(satypes.CHAR(length=1), default=A)

print [ _c.key for _c in MasterEntity.__table__.columns ]
# ['id', 'status']
# = the 'notes' column is missing

=== snip

So, what am I doing wrong?

Thanks in advance,
Frank

Btw.: Hello group and many thanks to Michael for this great piece of
software!



On Dec 16, 6:58 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 anabstractbaseclassisn't going to set up the same fields on all  
 descendants since the mapper()/Table() setup occurs during the  
 creation of the individualclassusing the non-inheritedclassdict,  
 and unique instances of each of the Column, etc. elements are required  
 as well.

 concrete inheritance, as referenced in that post, was not designed  
 to be used as a configurational spacesaver and always requires a  
 mapped selectable for the base, which you don't have here, so it's  
 not appropriate for this use case.

 So for this you'd need a custom metaclass:

 classMyMeta(DeclarativeMeta):
      def __init__(cls, classname, bases, dict_):
         cls.notes = Column(String)
         DeclarativeMeta.__init__(cls, classname, bases, dict_)

 Base= declarative_base(metaclass=MyMeta)

 On Dec 16, 2008, at 12:36 PM, Joril wrote:



  Hi everyone!
  I need to declare a few unrelated classes (from a business
  perspective) that share some attributes/fields, so I thought I could
  use anabstractclassto group these common attributes (note that just
  a few classes should have them, not every one)

  This post
 http://groups.google.it/group/sqlalchemy/msg/d3de02f609a0bbd9?hl=it
  suggests to use mixins, but I can't get it to work, SQLA generates the
  tables without the common fields.

  I'm testing it with this script:

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

  # --- Tables definition
 Base= declarative_base()

 classAbstract(object):
    notes = Column(String)

 classConcrete(Base,Abstract):
    __tablename__ = 'concrete'
    id = Column(Integer, primary_key=True)
  # ---

  # DB creation
  engine = create_engine('sqlite:///:memory:', echo=True)
 Base.metadata.create_all(engine)

  Here's the sql call:
  CREATE TABLE concrete (
         id INTEGER NOT NULL,
         PRIMARY KEY (id)
  )

  What am I missing? Did I misunderstood how the mixin should be used?
  (I'm using SQLA 0.5rc4)

  Many thanks!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Fwd: declarative_base and UNIQUE Constraint

2008-12-19 Thread FrankB

Hi,

just for anyone arriving here to save some time: I tried this with
0.5rc4 and the following piece of code

===

from sqlalchemy import types as satypes
from sqlalchemy import schema as saschema
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class MasterEntity(Base):
__tablename__ = master
id = saschema.Column(satypes.Integer, primary_key=True)
key= saschema.Column(satypes.Unicode(16))
entitytype = saschema.Column(satypes.String(32))
__mapper_args__ = {'polymorphic_on': entitytype,
'polymorphic_identity': 'master'}
__table_args__  = ((saschema.UniqueConstraint(entitytype, key),),
{})

===

and received the error

AttributeError: 'tuple' object has no attribute '_set_parent'.


Changing the last line to

__table_args__  = (saschema.UniqueConstraint(entitytype, key), {})

(means: removing the tuple) yields

KeyError: Column('entitytype', ...)

but this (means: put column names into quotes) eventually works:

__table_args__  = ( saschema.UniqueConstraint(entitytype,
key), {} )


Regards, Frank



-- Forwarded message --
From: Michael Bayer mike...@zzzcomputing.com
Date: Sep 15, 12:51 am
Subject: declarative_base and UNIQUE Constraint
To: sqlalchemy


format is __table_args__ = ((UniqueConstraint(),), {})

On Sep 14, 2008, at 1:49 PM, GustaV wrote:



 How do I create a unique constraint with the declarative plugin
 (latest version 0.5) ?

 both:
 __table_args__ = (UniqueConstraint('region.x', 'region.y'),
 {'mysql_engine':'InnoDB'} )
 __table_args__ = (UniqueConstraint(x, y), {'mysql_engine':'InnoDB'} )
 don't work.

 Thanks!


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---