On 7/26/2010 4:55 PM, Faheem Mitha wrote:

Hi,

It turns out my example was too hasty. I should have had something like

foobar = Table(
    'foo', meta,
    Column('id',  Integer, nullable=False, primary_key=True),
    )

bar = Table(
    'bar', meta,
    Column('id',  None, ForeignKey('foo.id', , ), nullable=False,  primary_key=True),
    )

baz = Table(
    'baz', meta,
    Column('id',  None, ForeignKey('bar.id', , ), nullable=False, primary_key=True),
    )

which also gives the same error. Using None type for ForeignKeys here, per the docs. My previous example was using None for a col that was not a ForeignKey.

I had a similar question a little while back and here was the answer:  http://groups.google.com/group/sqlalchemy/msg/89efcb84f25dcd28

Basically you shouldn't use None for a ForeignKey's type anymore.


Also, replacing the type of bar.id with Integer gives the error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) there is no unique constraint matching given keys for referenced table "bar"
 '\nCREATE TABLE baz (\n\tid INTEGER NOT NULL, \n\tPRIMARY KEY (id), \n\t FOREIGN KEY(id) REFERENCES bar (id) ON DELETE CASCADE ON UPDATE CASCADE\n)\n\n' {}

which is not very encouraging either. Currently trying to get sqla to emit the SQL for create_all so I can see what it is trying to do.

I added

db.echo = True
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.DEBUG)

but only the

db.echo = True

does something, and that doesn't emit the SQL for creating the tables.

I'm not sure what the problem here is, unless perhaps fiddling with the logging setup after enabling db.echo might be changing the output to not include the SQL commands?  All I really know about this is that if I set db.echo = True (actually I usually pass echo=True to the create_engine() call) then that's *all* I have to do to get SQL commands output to the console.  Perhaps someone else could clarify or maybe you could provide a complete example?

Lance


On Tue, 27 Jul 2010, Faheem Mitha wrote:


Dear SQLAlchemists,

With the following script, I get the traceback below. This is not the actual example I ran into, but a small test case. To my surprise, I was able to reproduce the identical error almost immediately. In my actual code, I was able to work around this error by doing a table.create() on the first table followed by a create_all, but that doesn't seem to work with my small example.

This is SQLAlchemy 0.5.8-1 running on Debian lenny with python 2.5.2-3, and with PostgreSQL 8.4.2-1~bpo50+1. I'm considering moving to 0.6 but am concerned about breakage.

This seems pretty innocuous. Clarifications appreciated.

                                                       Regards, Faheem.

***********************************************************************
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy import MetaData
meta = MetaData()

foo = Table(
   'foo', meta,
   Column('id',  None, nullable=False, primary_key=True),
   )

bar = Table(
   'bar', meta,
   Column('id',  None, ForeignKey('foo.id', ,
   ), nullable=False, primary_key=True),
   )

dbuser =
password =
dbname =
dbstring = "postgres://%s:%...@localhost:5432/%s"%(dbuser, password, dbname)
from sqlalchemy import create_engine
db = create_engine(dbstring)
meta.bind = db
meta.create_all()
Session = sessionmaker()
session = Session(bind=db)

**********************************************************
Traceback (most recent call last):
 File "<stdin>", line 23, in <module>
 File "/usr/lib/pymodules/python2.5/sqlalchemy/schema.py", line 1811, in create_all
   bind.create(self, checkfirst=checkfirst, tables=tables)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/engine/base.py", line 1129, in create
   self._run_visitor(self.dialect.schemagenerator, entity, connection=connection, **kwargs)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/engine/base.py", line 1158, in _run_visitor
   visitorcallable(self.dialect, conn, **kwargs).traverse(element)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/visitors.py", line 89, in traverse
   return traverse(obj, self.__traverse_options__, self._visitor_dict)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/visitors.py", line 200, in traverse
   return traverse_using(iterate(obj, opts), obj, visitors)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/visitors.py", line 194, in traverse_using
   meth(target)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/compiler.py", line 831, in visit_metadata
   self.traverse_single(table)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/visitors.py", line 79, in traverse_single
   return meth(obj)
 File "/usr/lib/pymodules/python2.5/sqlalchemy/sql/compiler.py", line 856, in visit_table
   self.append("\t" + self.get_column_specification(column, first_pk=column.primary_key and not first_pk))
 File "/usr/lib/pymodules/python2.5/sqlalchemy/databases/postgres.py", line 849, in get_column_specification
   colspec += " " + column.type.dialect_impl(self.dialect).get_col_spec()
 File "/usr/lib/pymodules/python2.5/sqlalchemy/types.py", line 392, in get_col_spec
   raise NotImplementedError()
NotImplementedError



--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to