On Friday, February 28, 2014 11:58:15 PM UTC-8, Chung WONG wrote: > > I have a *models.py* that contains 10 classes. And I am trying to move > all classes out of that file and put each class into its own file under a > models package. > > I am using the alternative(at bottom of page) method mentioned > here<https://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/database/sqlalchemy.html#importing-all-sqlalchemy-models>, > > but I don't know how it works for initialise db script as it is using > *config.scan()* > > However, when i ran *bin/initialize_bug_db development.in > <http://development.in> *to initialise the database, it threw such as : > > sqlalchemy.exc.InvalidRequestError: When initializing mapper > Mapper|User|users, expression 'TopicUser' failed to locate a name ("name > 'TopicUser' is not defined"). If this is a class name, consider adding this > relationship() to the <class 'bug.models.user.User'> class after both > dependent classes have been defined. > > which was caused by something like *topics = relationship('TopicUser', > backref="user", lazy='dynamic')* > > and > > sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column > 'notifications.topic_id' could not find table 'topics' with which to > generate a foreign key to target column 'id' > which was caused by something like *topic_id=Column(Integer, > ForeignKey('topics.id <http://topics.id>'))* > > > I am wondering how you guys organise the model files. Maybe there is a > better way? > > Thanks. >
In general: I would keep the model completely separate from the Pyramid Web application. In simpler projects, this would just mean creating a subpackage (like you're already doing) and being sure to not use any Pyramid stuff in the model (in particular, the request and registry thread locals). For more complex projects, I usually create an entirely separate package for the model that is entirely unaware of the Web app it's used in. In the configuration you linked, I think the problem is with the line `Base. metadata.create_all(engine)`. The database tables shouldn't be created every time the application is started. In a real production application, you almost certainly wouldn't do that (because you'd create the database up front and then use migrations when you change your schema). If you're going to use the `config.scan()` trick they show (and personally, I wouldn't), it'd probably be better to wrap it up in a `create_db()` script and call it only when you need to. -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pylons-discuss. For more options, visit https://groups.google.com/groups/opt_out.
