circular foreign key dependency doesn't work for me

Issue #352 resolved
Former user created an issue

I have downloaded the 0.3 version from sourceforge. I am completly new with sa, so maybe it is my fault.

I have pysqlite.version==1.0.1

I have made the following script according to wiki:New03Tricks .

from sqlalchemy import *
db = create_engine('sqlite:///tutorial.db')
meta = BoundMetaData(db)
t1 = Table('table1', meta,
   Column('id', Integer, primary_key=True),
   Column('t2_id', Integer, ForeignKey('t2.id'))
)
t2 = Table('table2', meta,
  Column('id', Integer, primary_key=True),
  Column('t1_id', Integer, ForeignKey('t1.id', use_alter=True, name='t2id_fk'))
)
meta.create_all()

I get the following traceback:

  File "saproba.py", line 17, in ?
    meta.create_all()
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 862, in create_all
    connectable.create(self, checkfirst=checkfirst, tables=tables)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/engine/base.py", line 413, in create
    self._run_visitor(self.dialect.schemagenerator, entity, connection=connection, **kwargs)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/engine/base.py", line 437, in _run_visitor
    element.accept_schema_visitor(visitorcallable(self, conn.proxy, connection=conn, **kwargs), traverse=False)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 882, in accept_schema_visitor
    visitor.visit_metadata(self)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/ansisql.py", line 635, in visit_metadata
    collection = [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))](t)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 846, in table_iterator
    return iter(sorter.sort(reverse=reverse))
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/sql_util.py", line 27, in sort
    self._sorted = self._do_sort()
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/sql_util.py", line 48, in _do_sort
    table.accept_schema_visitor(vis)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 267, in accept_schema_visitor
    c.accept_schema_visitor(visitor, True)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 479, in accept_schema_visitor
    f.accept_schema_visitor(visitor, traverse=True)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 571, in accept_schema_visitor
    visitor.visit_foreign_key(self)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/sql_util.py", line 42, in visit_foreign_key
    parent_table = fkey.column.table
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 567, in <lambda>
    column = property(lambda s: s._init_column())
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 551, in _init_column
    table = Table(tname, parenttable.metadata, mustexist=True, schema=schema)
  File "/home/bd/download/SQLAlchemy-0.3.0/lib/sqlalchemy/schema.py", line 132, in __call__
    raise exceptions.ArgumentError("Table '%s.%s' not defined" % (schema, name))sqlalchemy.exceptions.ArgumentError: Table 'None.t2' not defined

Comments (2)

  1. Mike Bayer repo owner

    two things - i named the foreign key tables incorrectly, check out the wiki "whats new in 0.3" page for the correction. also SQLite doesnt support ALTER so it wont work on sqlite.

  2. Mike Bayer repo owner

    oh you know what, nevermind, it works on sqlite. sqlite doesnt support "alter", but it lets you create the foreign keys inline against a non-existent table, i forgot about that. the sqlite dialect does handle this properly.

  3. Log in to comment