the clue here is that you're needing to call useexisting. this means a
Table that is already reflected will be pulled from the metadata. By adding
your own id column, that blows away the id that was already reflected,
which is what the addresses primary key is pointing to:
pdb at line 895 of properties.py
(Pdb) self.target
Table('address', MetaData(Engine(oracle://scott:ti...@localhost/xe)),
Column('id', Integer(), table=address, primary_key=True, nullable=False,
default=Sequence('address_sq', start=None, increment=None, optional=False)),
Column(u'name', VARCHAR(length=10, convert_unicode=False, assert_unicode=None,
unicode_error=None, _warn_on_bytestring=False), table=address),
Column(u'country_id', NUMBER(precision=None, scale=None, asdecimal=False),
ForeignKey(u'country.id'), table=address), Column(u'customer_id',
NUMBER(precision=None, scale=None, asdecimal=False),
ForeignKey(u'customer.id'), table=address), schema=None)
(Pdb) self.parent.mapped_table
Table(u'customer', MetaData(Engine(oracle://scott:ti...@localhost/xe)),
Column('id', Integer(), table=customer, primary_key=True, nullable=False,
default=Sequence('customer_sq', start=None, increment=None, optional=False)),
Column(u'name', VARCHAR(length=10, convert_unicode=False, assert_unicode=None,
unicode_error=None, _warn_on_bytestring=False), table=customer),
Column(u'customer_type_id', NUMBER(precision=None, scale=None,
asdecimal=False), ForeignKey(u'customer_type.id'), table=customer),
schema=None)
(Pdb) self.target.c.customer_id
Column(u'customer_id', NUMBER(precision=None, scale=None, asdecimal=False),
ForeignKey(u'customer.id'), table=address)
(Pdb) self.target.c.customer_id.references(self.parent.mapped_table.c.id)
False
(Pdb) self.target.c.customer_id.foreign_keys
OrderedSet([ForeignKey(u'customer.id')])
So the first part of the answer is to reverse the order in which you declare
Customer and Address, and don't use the 'useexisting' flag - leaving it off
instead ensures you are reflecting tables in their order of dependency.
Then the next issue is that you're defining relationships using class objects
in your metaclass - this triggers a premature mapper compilation issue and you
really should be using strings to reference classes at that level (i.e.
relationship (classobj.__name__)) . The RelationNameAttribute *way* oversteps
its bounds by actually constructing a full blown Query() object before all
classes are compiled - all of this has to occur using methods and descriptors
so that nothing is asked of the mapper setup until all classes are defined.
You then have to ensure your two declarative bases are sharing the same
_decl_class_registry. I'd move the two bases to use one declarative base and
have your metaclass check for attributes that indicate it should do its thing.
Also, trying to make declarative, which is so named because it is just that,
perform complex tasks implicitly is going to be difficult since it was not
designed for use cases like these.
On May 10, 2010, at 11:37 AM, GHZ wrote:
Hi,
Using plain Declarative, I am able to redefine a primary key column
that has been autoloaded, so that I can link it to an oracle sequence
and give it a new name:
Id = Column('id', Integer, Sequence('table_sq'), primary_key=True)
However, if I then try to add some methods to the class using a
metaclass, the foreign key relationships pointing to this column, seem
to go missing.
Apologies for not being able to track down the exact cause of this,
but it seems to be something I am doing wrong with the combination of
autloading, redefining the primary key, and adding to the class
through a metaclass.
The following example should work against an empty oracle schema.
I get: sqlalchemy.exc.ArgumentError: Could not determine join
condition between parent/child tables on relationship
Customer.addresses. Specify a 'primaryjoin' expression. If this is a
many-to-many relationship, 'secondaryjoin' is needed as well.
http://python.pastebin.com/7V8MEfH3
from sqlalchemy import MetaData, Column, Integer, Sequence,
ForeignKey, create_engine
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from sqlalchemy.ext.declarative import DeclarativeMeta,
declarative_base
engine = create_engine('oracle://fred:f...@mig01')
ddls = [
drop table customer,
drop table address,
drop table country,
drop table customer_type,
create table customer_type (
id number primary key,
name varchar2(10)),
create table country (
id number primary key,
name varchar2(10)),
create table customer (
id number primary key,
name varchar2(10),
customer_type_id number references customer_type),
create table address (
id number primary key,
name varchar2(10),
country_id number references country,
customer_id number