[sqlalchemy] declarative, reflection, and secondary tables.

2013-11-16 Thread Jon Nelson
I've been experiencing a weirdness.

Using SQLAlchemy 0.8.3 and PostgreSQL with declarative (and deferred
reflection), if I don't manually force a reflection *before* I call
prepare then - when I use certain objects - I get an error which
indicates that SQLAlchemy did not reflect secondary tables. Is this a
known issue? It's really hard to find an isolated test case.


InvalidRequestError: When initializing mapper
Mapper|ObjectOne|table_one, expression 'table_one_secondary_table'
failed to locate a name (name 'table_one_secondary_table' is not
defined). If this is a class name, consider adding this
relationship() to the class 'ObjectOne class after both dependent
classes have been defined.


the table 'table_one_secondary_table' is like this:

class ObjectOne(Base):
  __tablename__ = 'table_one'
  
  object_twos = relationship(
ObjectTwo, backref='object_one',
cascade=save-update,merge,refresh-expire,expunge,
secondary='table_one_secondary_table',
passive_deletes=True
  )

-- 
Jon
Software Blacksmith

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] declarative, reflection, and secondary tables.

2013-11-16 Thread Michael Bayer

On Nov 16, 2013, at 4:08 PM, Jon Nelson jnel...@jamponi.net wrote:

 I've been experiencing a weirdness.
 
 Using SQLAlchemy 0.8.3 and PostgreSQL with declarative (and deferred
 reflection), if I don't manually force a reflection *before* I call
 prepare then - when I use certain objects - I get an error which
 indicates that SQLAlchemy did not reflect secondary tables. Is this a
 known issue? It's really hard to find an isolated test case.


that actually sounds like a real issue / missing feature since I’m not 
recalling prepare() is going to hit the “secondary” tables also.   

it should be easy to reproduce….(let’s see)….sure, simple.  this includes a 
workaround.  a new ticket with a patch is at 
http://www.sqlalchemy.org/trac/ticket/2865

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection


Base = declarative_base(cls=DeferredReflection)

class A(Base):
__tablename__ = 'a'
bs = relationship(B, secondary=atob)

class B(Base):
__tablename__ = 'b'


e = create_engine(sqlite://, echo=True)
e.execute(
create table a(id integer primary key)
)
e.execute(
create table b(id integer primary key)
)
e.execute(
create table atob(
a_id integer references a(id),
b_id integer references b(id),
primary key (a_id, b_id)
)
)

Base.prepare(e)

# if this isn't done, failure
#Table('atob', Base.metadata, autoload=True, autoload_with=e)

print A.bs.__clause_element__()

 
 
 InvalidRequestError: When initializing mapper
 Mapper|ObjectOne|table_one, expression 'table_one_secondary_table'
 failed to locate a name (name 'table_one_secondary_table' is not
 defined). If this is a class name, consider adding this
 relationship() to the class 'ObjectOne class after both dependent
 classes have been defined.
 
 
 the table 'table_one_secondary_table' is like this:
 
 class ObjectOne(Base):
  __tablename__ = 'table_one'
  
  object_twos = relationship(
ObjectTwo, backref='object_one',
cascade=save-update,merge,refresh-expire,expunge,
secondary='table_one_secondary_table',
passive_deletes=True
  )
 
 -- 
 Jon
 Software Blacksmith
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail