Thank you for your kind help; it was really helpful.
On Fri, Sep 13, 2013 at 2:40 AM, Michael Bayer <[email protected]>wrote: > > On Sep 11, 2013, at 10:36 PM, Joe Martin <[email protected]> wrote: > > Thank you for your reply. Then I thought the following would work: > > company_schema = 'c' + str(company_id) > db.session.execute(CreateSchema(company_schema)) > db.session.commit() > meta = db.MetaData(bind=db.engine) > for table in db.metadata.tables.items(): > if table[1].name == 'customer': > table[1].tometadata(meta, company_schema) > elif table[1].name == 'company': > table[1].tometadata(meta, 'app') # or > table[1].tometadata(meta)? > print meta.tables.items() > meta.create_all() > > Now I see print meta shows 2 tables, but somehow error is still the same: > "NoReferencedTableError: Foreign key associated with column > 'customer.company_id' > could not find table 'company' with which to generate a foreign key to > target column 'id' " > > However, with my original metadata I was able to create both tables: > app.company and public.customer. > So, I'm confused with the issue. Thanks for your time. > > > The way it works is this: > > a Table object that has a ForeignKey() object inside of it, is going to > want to find the target column that this ForeignKey points to. > > There are two ways to specify it. One is via string: > > ForeignKey("<schemaname>.tablename.columname") > > if you use that approach, the MetaData for this table *must have a Table > with exactly that target name present*. The table[1].name == 'company' > conditional you have seems to be doing this, but then the error you're > showing me doesn't include "app" inside of it, so perhaps you want to do > tometadata(meta, None), not sure. > > the other way, which might make this all easier, is to put the actual > Column object in the ForeignKey: > > ForeignKey(my_company_table.c.id) > > if you do it that way, then you can go back to your original approach > where you don't copy "company" at all - if ForeignKey is given the Column > directly, then no string lookup in the MetaData is needed and you can point > to any Table anywhere. > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" 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/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
