I'm using SQLAlchemy, reflecting from an existing MySQL database. I
want to override two DateTime columns to provide proper "created" and
"updated" timestamps (since MySQL can't handle auto-updating two
TIMESTAMP columns in the same row).

According to the SA docs, this should work; however, when I autoload
my Table objects, I get the error:

<class 'sqlalchemy.exceptions.ArgumentError'>: Table 'tablename' is
already defined for this MetaData instance.

This short example illustrates the issue; the test_users table fails
to load.  The error goes away if I either remove the foreign key
constraints in the 'test_pets' table, or remove the Column overrides
from the 'test_users' table.

It seems as if SA is instantiating the users mapper first (because the
pets table refers to it), but not paying attention to the override; it
then tries to instantiate the users mapper to effect the override, but
fails.

Thanks in advance...

##########
from sqlalchemy import Table, Column, MetaData, create_engine, func,
ForeignKey
from sqlalchemy.orm import mapper
from sqlalchemy.types import DateTime, Integer, String
__engine = create_engine('mysql://user:[EMAIL PROTECTED]/test')

metadata = MetaData()
metadata.bind = __engine

# Create the tables for example's sake; in production, they
# already exist.
users = Table('test_users', metadata,
              Column('id', Integer, primary_key=True),
              Column('name', String(40), nullable=False),
              Column('created_at', DateTime, nullable=False),
              Column('updated_at', DateTime, nullable=False),
              mysql_engine='InnoDB')


pets = Table('test_pets', metadata,
             Column('id', Integer, primary_key=True),
             Column('name', String(40), nullable=False),
             Column('user_id', Integer, ForeignKey('test_users.id'),
nullable=False),
             Column('created_at', DateTime, nullable=False),
             Column('updated_at', DateTime, nullable=False),
             mysql_engine='InnoDB')

metadata.drop_all()
metadata.create_all()
metadata.clear()

pets = Table('test_pets', metadata,
                   Column('created_at', DateTime, default=func.now()),
                   Column('updated_at', DateTime, default=func.now(),
onupdate=func.now()),
                   autoload=True)
users = Table('test_users', metadata,
                   Column('created_at', DateTime, default=func.now()),
                   Column('updated_at', DateTime, default=func.now(),
onupdate=func.now()),
                   autoload=True)

class Pet(object):
    pass

class User(object):
    pass

mapper(Pet, pets)
mapper(User, users)
##########

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