I have put my problem in a small program for easy understanding.
Run this with MySQL.
from sqlalchemy import create_engine, __version__
from sqlalchemy import Table, Column, Integer, String, MetaData,
ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
print "SQLAlchemy Version:", __version__
engine = create_engine('mysql://username:passw...@localhost/testdb',
echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
metadata = Base.metadata

class LotsOfNames(Base):
    __tablename__ = 'lotsofnames'
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset':
'utf8'}

    id = Column(Integer, primary_key=True)
    name = Column(String(255), ForeignKey('users.name'))

class User(Base):
    __tablename__ = 'users'
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset':
'utf8'}

    id = Column(Integer, primary_key=True)
    name = Column(String(255), primary_key=True)
    fullname = Column(String(255))
    password = Column(String(255))
    morenames = relationship(LotsOfNames,
                            backref=backref('lotsofnames',
                            order_by=LotsOfNames.id))

User.__table__.drop(bind=engine, checkfirst=True)
LotsOfNames.__table__.drop(bind=engine, checkfirst=True)
metadata.create_all(engine)

On Nov 5, 10:16 am, Richie Ward <rich...@gmail.com> wrote:
> I think I am using the same declarative_base instance but it is still
> making the tables in the wrong order.
>
> My model is 
> at:http://bazaar.launchpad.net/~richies/hypernucleus-server/PylonsPortEx...
>
> And here is what happens when I run: paster setup-app 
> development.inihttp://pastebin.com/VuW7UK3B
>
> The problem now appears at games.py which is very similar to the
> dependencies.py model.
> A declarative_base instance is created in meta.py and then all the
> models import the instance from that.
>
> The problem is also exacerbated by InnoDB which keeps referential
> integrity.
> I wanted to move to InnoDB for this very reason.
>
> On Nov 5, 12:29 am, Gunnlaugur Briem <gunnlau...@gmail.com> wrote:
>
> > Strictly that's not a query, it's a table definition. Do you mean that
> > you are creating a declarative model class corresponding to this table
> > definition? Posting your code would help more.
>
> > You must define both tables on the same metadata instance (in
> > declarative, that's typically done by having both model classes extend
> > the same declarative_base instance). If you call create_all on that
> > metadata instance, it does respect the dependency order. (Unless
> > you've found a bug, which is unlikely for code that's as central and
> > heavily used as this.)
>
> > Seehttp://www.sqlalchemy.org/docs/orm/extensions/declarative.html---
> > follow the beginning of that, and make sure both of your model classes
> > extend the same Base instance.
>
> > Regards,
>
> > - Gulli
>
> > On Nov 4, 10:51 pm, Richie Ward <rich...@gmail.com> wrote:
>
> > > I am trying to run this query:
> > > CREATE TABLE dependenciesbinary (
> > >     id INTEGER NOT NULL AUTO_INCREMENT,
> > >     dependency_mn VARCHAR(128),
> > >     name VARCHAR(128),
> > >     operatingsystem VARCHAR(128),
> > >     architecture VARCHAR(128),
> > >     PRIMARY KEY (id),
> > >     FOREIGN KEY(dependency_mn) REFERENCES dependencies (modulename)
> > > )ENGINE=InnoDB CHARSET=utf8
>
> > > But create_all() is not creating the table "dependencies" before
> > > "dependenciesbinary" which causes MySQL to error due to the missing
> > > table.
>
> > > Is there some way I can change the order of the create statements to
> > > fix this?
>
> > > I am using Declarative if that helps.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to