Chung,

I believe you are running into a similar problem I had recently when I
wanted to change the structure of my models folders for my application.
What I ended up doing is moving each model into its own file and importing
each of the files into packages __init__.py file.
Something like this:

application
|-- models
     |--- __init__.py
     |--- meta.py
     |--- foo
     |--- bar
     |--- baz


and in your init file, you would have the following imports:

from .foo import Foo
from .bar import Bar
from .baz import Baz

At this point, if you are still receiving the same exception when you are
initializing the database, make sure the order of the imports in the
__init__.py file matches the order of which the dependent tables should be
created. In your case, make sure you are importing User before you import
TopicUser, for example.

Hope this gives you some direction!

-Vincent


On Sat, Mar 1, 2014 at 12:58 AM, Chung WONG <[email protected]> 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.
>
> --
> 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.
>



-- 
Vincent Catalano
Software Engineer and Web Ninja,
(520).603.8944

-- 
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.

Reply via email to