I have a User class, and a Registration class with a FK to User.id.

When I try to create these on a db using InnoDB as default, I get this 
error:

sqlalchemy.exc.OperationalError: (OperationalError) (1005, "Can't create 
table 'test2.registrations' (errno: 150)") '\nCREATE TABLE 
test2.registrations (\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\tuser_id 
INTEGER, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(user_id) REFERENCES 
user_vars (id)\n)\n\n' ()

If i copy this query and try it manually via MySQL Workbench, it still 
fails. 

If I prepend the default schema to the table (user_vars -> 
'test.user_vars'), the query succeeds.

I'm not entirely sure what is happening here. In the absence of an explicit 
schema, I thought mysql used the active schema to handle table lookups, but 
it looks like this isn't the case. Is it 'switching' the active schema to 
the one hosting the new table? Also, this works perfectly with MyISAM 
tables, so I have even less to go on.

Any ideas? Do I need explicit schema declarations for every fk declared?

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
import sys

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('mysql://name:password@localhost/test')
Base = declarative_base(bind=engine)


class User(Base):
    __tablename__ = 'user_vars'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))

class Registration(Base):
    __tablename__ = 'registrations'
    __table_args__ = {
        'schema': 'test2'
        }
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey(User.id))


if __name__ == '__main__':
    Base.metadata.drop_all()
    Base.metadata.create_all()

Reply via email to