In my various models, I recently noticed that I have been reusing my
customer table as it relates to many other models.  I've been making
CustomerPart objects in each of the model files and retyping the table
schema and object (only including the pieces I need for that
particular case)

Now I'd like to have one customer model and let the other models
access it -- rather than continually retyping everything.

But I get this error when I break it out (this is when I try to use it
in a controller in pylons):
  raise exceptions.InvalidRequestError("Could not find table '%s' with
which to generate a foreign key" % tname)
InvalidRequestError: Could not find table 'customer' with which to
generate a foreign key

I've included a before and after (hooray for GIT) of the models:

Thanks!

Matt



saq.py before (this one works)
----------------------------------------------------
from sqlalchemy import Column, MetaData, Table, types, ForeignKey,
func
from sqlalchemy.orm import mapper, relation

from datetime import datetime

from formencode import validators
from smmodels import fe_obj, NoHTML, SuperDateValidator, fe_setter

metadata = MetaData()

...

customer_table_part = Table('customer', metadata,
    Column('id', types.Integer, primary_key=True, index=True),
    Column('email', types.Unicode, unique=True, index=True),
    Column('validation_type', types.Unicode),
)

questionnaire_table = Table('saq_questionnaire', metadata,
...
    Column('customer_id', types.Integer, ForeignKey('customer.id'),
index=True),
...
)

questions_table = Table('saq_questions_new', metadata,
...
)


class Questionnaire(fe_setter):

    def __str__(self):
        return 'id: %s customer_id: %s' % (self.id, self.customer_id)

    def __repr__(self):
        return "<Questionnaire(%s, customer_id:%s)>" % (self.id,
self.customer_id)


class Question(fe_setter):
    pass


class CustomerPart(fe_setter):

    def __init__(self):
        pass


mapper(Question, questions_table)
mapper(CustomerPart, customer_table_part)
mapper(Questionnaire, questionnaire_table, properties={
    ...
    'customer'  :relation(CustomerPart, backref='questionnaires')
})





saq.py after (all the same except removed references to CustomerPart
----------------------------------------------------
...
from smmodels.customer import customer_table, Customer
...

mapper(Question, questions_table)
mapper(Questionnaire, questionnaire_table, properties={
    ...
    'customer'  :relation(Customer, backref='questionnaires')
})


customer.py after (newly created)
----------------------------------------------------
from sqlalchemy import Column, MetaData, Table, types, ForeignKey
from sqlalchemy.orm import mapper, relation

from formencode import validators
from smmodels import fe_obj, NoHTML, fe_setter

from datetime import date

metadata = MetaData()

customer_table = Table('customer', metadata,
    Column('id', types.Integer, primary_key=True, index=True),
    Column('email', types.Unicode, unique=True, index=True),
    Column('validation_type', types.Unicode),
)

class Customer(object):

    def __init__(self):
        pass

mapper(Customer, customer_table)
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to