OK, dumb error, move Base.metadata.create_all(engine)  below table1 =
tablemeta("table1")  and it works.

This works when I create the columns via the temp dict in __new__ not
when I do so in __init__ (it complains about missing __tabledata__),
but why?

On Mar 18, 6:47 pm, farcat <[email protected]> wrote:
> Well,
>
> (I am not a exactly a beginner in Python, just a bit rusty i
> guess :-) )  I am working on a way to persist multiple inheritance and
> polymorphism in a database, but I am a beginner with SQLalchemy.
>
> Anyway I have a new problem for which i would like some help:
>
> The following code gives an error:
>
> class tablemeta(DeclarativeMeta):
>     def __new__(cls, name):
>         temp = dict()
>         temp["__tablename__"] =  "_" + name
>         temp["id"] = Column(Integer, primary_key = True)
>         temp["text"] = Column(String(120))
>         return DeclarativeMeta.__new__(cls, name, (Base,), temp)
>     def __init__(cls, name):
>         return DeclarativeMeta.__init__(cls, name, (Base,),{})
>
> if __name__ == "__main__":
>     engine = create_engine('sqlite:///tempDB.db', echo=True)
>     Base.metadata.drop_all(engine)
>     Base.metadata.create_all(engine)
>     Session = sessionmaker(bind=engine)
>     session = Session()
>     table1 = tablemeta("table1")
>     row1 = table1(text = "detextenzo")
>     row2 = table1(text = "detextenzoennogeenbeetje")
>     session.add(row1)
>     session.add(row2)
>     session.commit()
>     list = session.query("table1").all()   #<== ERROR
>     for l in list:
>         print str(l)
>
>  File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py",
> line 299, in do_execute
>     cursor.execute(statement, parameters)
> sqlalchemy.exc.OperationalError: (OperationalError) no such table:
> _table1 u'INSERT INTO _table1 (text) VALUES (?)' ('detextenzo',)
>
> Seems an odd error, any ideas?
>
> On Mar 17, 6:20 pm, Chris Withers <[email protected]> wrote:
>
>
>
>
>
>
>
> > On 16/03/2011 21:01, farcat wrote:
>
> > > I have an error i cant figure out (likely a beginners error):
>
> > Beginners should not be using metaclasses.
>
> > What's your use case here?
>
> > Chris
>
> > --
> > Simplistix - Content Management, Batch Processing & Python Consulting
> >             -http://www.simplistix.co.uk

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

Reply via email to