I am working on my localization stuff and run into a problem when I want to add an relationship to a class.

I based my code on http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Views , but maybe I messed something up.

The following works:

r1 = session.query(db.Region_LV).get(175)
print type(result.country_lv)
print r1
print r1.country_lv.name

with this output:
Region_LV(centralkey=175, country_lv=Country_LV(centralkey=83, created_at=datetime.datetime(2011, 9, 7, 9, 39, 10, 702000), created_by=None, dialcode=30, fk_language_id=2, id=83, is2code=u'GR', iswinecountry=1, name=u'Gr\xe8ce', shortname=None, un3code=u'GRC', updated_at=datetime.datetime(2011, 9, 7, 9, 39, 10, 702000), updated_by=None, website1=None, website2=None), created_at=datetime.datetime(2011, 9, 7, 9, 39, 11, 452000), created_by=None, fk_country_id=83, fk_language_id=2, id=175, name=u'Sud-Ouest', shortname=u'Sud-Ouest', updated_at=datetime.datetime(2011, 9, 7, 9, 39, 11, 452000), updated_by=None)
Grèce

The model for all this is:

class Region(DeclarativeBase, mix.StandardColumnMixin):
    __tablename__ = u'region'

    centralkey = sa.Column(sa.BigInteger())
    name = sa.Column(sa.Unicode(length=50), nullable=False)
    shortname = sa.Column(sa.Unicode(length=10))
    fk_country_id = sautils.reference_col('country')

    __localize_columns__ = ['name', 'shortname']


Region_L, Region_LV = sautils.CreateLocalized(Region())

Region_LV.country_lv = sao.relationship('Country_LV')

CreateLocalized creates the "Region_LV" based on the usage recipe.

Now if I add:
Region_LV.language = sao.relationship('Language')

I get:
sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/child tables on relationship Region_LV.language. Specify a 'primaryjoin' expression. If 'secondary' is present, 'secondaryjoin' is needed as well.

or:
Region_LV.language = sao.relationship('Language',
                primaryjoin="Region_LV.fk_language_id==Language.id")

I get:
sqlalchemy.exc.ArgumentError: Could not determine relationship direction for primaryjoin condition 'country_lv.fk_language_id = language.id', on relationship Region_LV.language. Ensure that the referencing Column objects have a ForeignKey present, or are otherwise part of a ForeignKeyConstraint on their parent Table, or specify the foreign_keys parameter to this relationship.

or:
Region_LV.language = sao.relationship('Language',
                   primaryjoin="Country_LV.fk_language_id==Language.id",
                   foreign_keys=[Country_LV.__table__.c.fk_language_id])

I get:
sqlalchemy.exc.ArgumentError: Column-based expression object expected for argument 'foreign_keys'; got: 'Country_LV.fk_language_id', type <type 'str'>

sqlalchemy.exc.ArgumentError: Could not locate any foreign-key-equated, locally mapped column pairs for primaryjoin condition 'country_lv.fk_language_id = language.id' on relationship Region_LV.language. For more relaxed rules on join conditions, the relationship may be marked as viewonly=True.

Tried with "viewonly" but couldn't make that work either.

I guess/think my problem is that I don't define a ForeignKeyConstraint for the "fk_language_id" column but I haven't found how this is done as the Country_LV view is created using "from sqlalchemy.sql import table".

I'd appreciate any tips on how to get this to work.

Werner

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

Reply via email to