On Aug 23, 2010, at 11:04 AM, keekychen.shared wrote:
> Dear All,
>
> I have questions in coding my application using sqlalchemy on database
> "tables".
>
>
> 2. how to duplicate a table with name in sqlalchemy?
> I posted a question in
> "http://stackoverflow.com/questions/3545140/sqlalchemy-duplicate-table-rename-table"
> but seems still no answer.
method 1 - produce your Table object with a callable:
def new_table(name, metadata):
return Table(name, metadata,
Column('id',Integer,primary_key=True),
Column('name',String),
Column('fullname',String),
Column('password',String)
)
users = new_table('users', metadata)
users001 = new_table('users_001', metadata)
method 2 - adapt the source code from the "tometadata()" method of Table, which
copies a Table to a new MetaData. This makes use of the copy() method of
Column and Constraint.
def copy_my_table(name, table):
args = []
for c in table.columns:
args.append(c.copy())
for c in table.constraints:
args.append(c.copy())
return Table(name, table.metadata, *args)
>
> 3. how to rename a table in sqlalchemy?
you must issue the appropriate ALTER statements to your database to change the
name of the table. As far as the Table metadata itself, you can attempt to
set table.name = 'newname', and re-place the Table object within
metadata.tables with its new name, but this may have lingering side effects
regarding foreign keys that reference the old name. In general, the pattern is
not supported - its intended that a SQLAlchemy application runs with a fixed
database structure (only new tables can be added on the fly).
You can also check out the Migrate project which allows migrations, but only in
the context of a migration script, not a running application.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.