On Sun, Oct 12, 2008 at 3:00 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > Hi, I am following the tutorial at: > http://wiki.pylonshq.com/display/pylonscookbook/Making+a+Pylons+Blog > but when it tells me how to set the model up, I run into some issues. > > > Can anyone tell me which of these two setups I should be using? > > > ############################### > # WHAT I HAVE > ############################### > > import sqlalchemy as sa > from sqlalchemy import orm > from sqlalchemy import types > > def init_model(bind): > """Call me at the beginning of the application. > 'bind' is a SQLAlchemy engine or connection, as returned by > sa.create_engine, sa.engine_from_config, or engine.connect(). > """ > global engine, Session > engine = bind > Session = orm.scoped_session( > orm.sessionmaker(transactional=True, autoflush=True, > bind=bind)) > orm.mapper(Blog, blog_table, > order_by=[blog_table.c.date.desc()]) > > meta = sa.MetaData() > > blog_table = sa.Table("Blog", meta, > sa.Column("id", types.Integer, primary_key=True, > autoincrement=True), > sa.Column("subject", types.String(255)), > sa.Column("author", types.String(255)), > sa.Column("date", types.DateTime()), > sa.Column("content", types.Text()), #or types.TEXT() - to .Text() > doesn't exists :/ > ) > > class Blog(object): > def __str(self): > return self.title > > ############################### > # WHAT I AM TOLD TO USE: > ############################### > > """The application's model objects""" > import sqlalchemy as sa > from sqlalchemy import orm > > from myblog.model import meta > > def init_model(engine): > """Call me before using any of the tables or classes in the > model""" > ## Reflected tables must be defined and mapped here > #global reflected_table > #reflected_table = sa.Table("Reflected", meta.metadata, > autoload=True, > # autoload_with=engine) > #orm.mapper(Reflected, reflected_table) > > sm = orm.sessionmaker(autoflush=True, transactional=True, > bind=engine) > > meta.engine = engine > meta.Session = orm.scoped_session(sm) > > > ## Non-reflected tables may be defined and mapped at module level > #foo_table = sa.Table("Foo", meta.metadata, > # sa.Column("id", sa.types.Integer, primary_key=True), > # sa.Column("bar", sa.types.String(255), nullable=False), > # ) > # > #class Foo(object): > # pass > # > #orm.mapper(Foo, foo_table) > > > ## Classes for reflected tables may be defined here, but the table and > ## mapping itself must be done in the init_model function > #reflected_table = None > # > #class Reflected(object): > # pass > > ############################### > # WHAT I GET IF I USE THE SECOND > ############################### > > Starting subprocess with file monitor > C:\Documents and Settings\James\My Documents\MyBlog\myblog\model > \__init__.py:13: > SADeprecationWarning: The 'transactional' argument to sessionmaker() > is depreca > ted; use autocommit=True|False instead. > orm.sessionmaker(transactional=True, autoflush=True, bind=bind)) > Traceback (most recent call last): > File "C:\Python25\scripts\paster-script.py", line 8, in <module> > load_entry_point('pastescript==1.6.3', 'console_scripts', 'paster') > () > File "c:\python25\lib\site-packages\pastescript-1.6.3-py2.5.egg\paste > \script\c > ommand.py", line 79, in run > invoke(command, command_name, options, args[1:]) > File "c:\python25\lib\site-packages\pastescript-1.6.3-py2.5.egg\paste > \script\c > ommand.py", line 118, in invoke > exit_code = runner.run(args) > File "c:\python25\lib\site-packages\pastescript-1.6.3-py2.5.egg\paste > \script\c > ommand.py", line 213, in run > result = self.command() > File "c:\python25\lib\site-packages\pastescript-1.6.3-py2.5.egg\paste > \script\s > erve.py", line 251, in command > relative_to=base, global_conf=vars) > File "c:\python25\lib\site-packages\pastescript-1.6.3-py2.5.egg\paste > \script\s > erve.py", line 278, in loadapp > **kw) > File "c:\python25\lib\site-packages\pastedeploy-1.3.2-py2.5.egg\paste > \deploy\l > oadwsgi.py", line 204, in loadapp > return loadobj(APP, uri, name=name, **kw) > File "c:\python25\lib\site-packages\pastedeploy-1.3.2-py2.5.egg\paste > \deploy\l > oadwsgi.py", line 225, in loadobj > return context.create() > File "c:\python25\lib\site-packages\pastedeploy-1.3.2-py2.5.egg\paste > \deploy\l > oadwsgi.py", line 625, in create > return self.object_type.invoke(self) > File "c:\python25\lib\site-packages\pastedeploy-1.3.2-py2.5.egg\paste > \deploy\l > oadwsgi.py", line 110, in invoke > return fix_call(context.object, context.global_conf, > **context.local_conf) > File "c:\python25\lib\site-packages\pastedeploy-1.3.2-py2.5.egg\paste > \deploy\u > til\fixtypeerror.py", line 57, in fix_call > val = callable(*args, **kw) > File "C:\Documents and Settings\James\My Documents\MyBlog\myblog > \config\middle > ware.py", line 34, in make_app > load_environment(global_conf, app_conf) > File "C:\Documents and Settings\James\My Documents\MyBlog\myblog > \config\enviro > nment.py", line 49, in load_environment > model.init_model(engine) > File "C:\Documents and Settings\James\My Documents\MyBlog\myblog > \model\__init_ > _.py", line 15, in init_model > order_by=[blog_table.c.date.desc()]) > File "c:\python25\lib\site-packages\sqlalchemy-0.5.0rc2-py2.5.egg > \sqlalchemy\o > rm\__init__.py", line 670, in mapper > return Mapper(class_, local_table, *args, **params) > File "c:\python25\lib\site-packages\sqlalchemy-0.5.0rc2-py2.5.egg > \sqlalchemy\o > rm\mapper.py", line 196, in __init__ > self.__compile_class() > File "c:\python25\lib\site-packages\sqlalchemy-0.5.0rc2-py2.5.egg > \sqlalchemy\o > rm\mapper.py", line 857, in __compile_class > self.class_) > sqlalchemy.exc.ArgumentError: Class '<class 'myblog.model.Blog'>' > already has a > primary mapper defined. Use non_primary=True to create a non primary > Mapper. cl > ear_mappers() will remove *all* current mappers from all classes. > > > >
They're both the same; it's just that the second puts the Session and metadata in another module. We switched to that to avoid circular imports if the model is put in several modules. The tutorial may have been written just before that point. The error message means what it says. You've called orm.mapper twice on the class. You need to call it either inside init_model() or outside, but not both. If you're autoloading the column types from the database, you'll have to create the table and call orm.mapper inside init_model. But if you define all the columns literally as you do here, you can put the mapper call either place. -- Mike Orr <[EMAIL PROTECTED]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" 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/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---
