Copy of a table with a different name

131 views
Skip to first unread message

Artur Siekielski

unread,
Jun 12, 2008, 4:05:56 AM6/12/08
to sqlalchemy
Hi.
I want to have two Table definitions in one MetaData which are the
same except the name of the second one has "SND_" prefix. To avoid
duplication of schema definition I looked at Table.tometadata() source
and created the following function:

def _cloneToSND(table, metadata):
return Table('SND_' + table.name, metadata, *([c.copy() for c in
table.columns] + \
[c.copy() for c in table.constraints]))

But calling metadata.create_all() ends with error:

metadata.create_all(e)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/schema.py", line 1582, in create_all
bind.create(self, checkfirst=checkfirst, tables=tables)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/engine/base.py", line 1139, in create
self._run_visitor(self.dialect.schemagenerator, entity,
connection=connection, **kwargs)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/engine/base.py", line 1168, in _run_visitor
visitorcallable(self.dialect, conn, **kwargs).traverse(element)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 75, in traverse
return self._non_cloned_traversal(obj)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 134, in
_non_cloned_traversal
self.traverse_single(target)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 35, in traverse_single
return meth(obj, **kwargs)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/compiler.py", line 756, in visit_metadata
collection = [t for t in metadata.table_iterator(reverse=False,
tables=self.tables) if (not self.checkfirst or not
self.dialect.has_table(self.connection, t.name, schema=t.schema))]
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/schema.py", line 1456, in table_iterator
return iter(sort_tables(tables, reverse=reverse))
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/util.py", line 21, in sort_tables
vis.traverse(table)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 75, in traverse
return self._non_cloned_traversal(obj)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 134, in
_non_cloned_traversal
self.traverse_single(target)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/visitors.py", line 35, in traverse_single
return meth(obj, **kwargs)
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/sql/util.py", line 15, in visit_foreign_key
parent_table = fkey.column.table
File "/usr/share/python2.5/site-packages/SQLAlchemy-0.4.6dev_r4841-
py2.5.egg/sqlalchemy/schema.py", line 788, in column
"foreign key" % tname)
sqlalchemy.exceptions.NoReferencedTableError: Could not find table
'ObjectType' with which to generate a foreign key


The first table (without SND_ prefix) was created successfully. Any
ideas how to achieve my goal?

Regards,
Artur

Michael Bayer

unread,
Jun 12, 2008, 10:15:22 AM6/12/08
to sqlal...@googlegroups.com

send along a test case that includes whatever ForeignKey references to/
from ObjectType might be involved here. My initial guess might be
to lose the "constraints.copy()" section since each Column.copy() will
contain a copied ForeignKey inside of it. copy() has only been used
by the to_metadata() method up til this point.

Artur Siekielski

unread,
Jul 17, 2008, 4:57:46 AM7/17/08
to sqlalchemy
> send along a test case that includes whatever ForeignKey references to/
> from ObjectType might be involved here.    My initial guess might be  
> to lose the "constraints.copy()" section since each Column.copy() will  
> contain a copied ForeignKey inside of it.  copy() has only been used  
> by the to_metadata() method up til this point.

Sorry for not replying for a month.
I'm trying to produce a test case using simple tables. I use two
MetaDatas but what I get is

sqlalchemy.exceptions.NoReferencedTableError: Could not find table 'A'
with which to generate a foreign key

In production code I use foreign keys between tables in different
metadatas (I use one MetaData per "component"). Is it ok and should
work?

Here is the complete code:

#!/usr/bin/python

from sqlalchemy import Table, Column, Integer, Text, MetaData,
ForeignKey, Date, DateTime, Float
from sqlalchemy.engine import create_engine
from sqlalchemy.orm import mapper, relation, backref

engine = create_engine('sqlite://')

metadata_1 = MetaData()
metadata_2 = MetaData()

def _cloneToSND(table, metadata):
return Table('SND_' + table.name, metadata,
*([c.copy() for c in table.columns] + \
[c.copy() for c in table.constraints]))

tableA = Table('A', metadata_1,
Column('id', Integer, primary_key=True))

tableB = Table('B', metadata_1,
Column('id', Integer, primary_key=True),
Column('a_id', Integer, ForeignKey(tableA.c.id),
nullable=True, index=True))

tableC = Table('C', metadata_2,
Column('id', Integer, primary_key=True),
Column('a_id', Integer, ForeignKey(tableA.c.id),
nullable=True, index=True))
#Column('b_id', Integer, ForeignKey(tableB.c.id),
nullable=True, index=True))

tableSND_A = _cloneToSND(tableA, metadata_1)
tableSND_B = _cloneToSND(tableB, metadata_1)
tableSND_C = _cloneToSND(tableC, metadata_2)

if __name__ == '__main__':
metadata_1.create_all(bind=engine)
metadata_2.create_all(bind=engine)
Reply all
Reply to author
Forward
0 new messages