New to sqlalchemy multiple files one to one and one to many - circular relationship issues

1,041 views
Skip to first unread message

Paul Kraus

unread,
Jan 6, 2012, 10:15:02 PM1/6/12
to sqlal...@googlegroups.com
I have an address class that i would like to use in several locations for instance vendors need addresses and customers need addresses.

I have my model split between 2 files globals and ar. A customer can have a default bill to address, default ship to address, and multiple address to choose from for ship to's beyond the default. I have the bill_to and ship_to default working fine but for the life of me can't figure out how to create the locations reference. I know how to do it if i put it on the address class but then i have python import issues obviously. I could put them in the same file but then i lose the versalitity of having the same kind of address setup for vendors (defaults and multiples locations also). How can I define "locations" that would be a list of addresses on the customer class.

Hope this makes sense. TIA

AR model ..
from erp.model.globals import Address

class Customer(DeclarativeBase):
    __tablename__ = 'customers'
    customer_id = Column(Integer, primary_key=True)
    customer_name = Column(Unicode(100))
    discount = Column(Float)
    #bill_to_id = Column(Integer, ForeignKey('addresses.address_id'))
    #bill_to = relation(Address,primaryjoin=bill_to_id==Address.address_id,uselist=False)
    ship_to_id = Column(Integer, ForeignKey('addresses.address_id'))
    ship_to = relation(Address,primaryjoin=ship_to_id==Address.address_id,uselist=False)
   
globals ...
rom erp.model import DeclarativeBase, metadata, DBSession

class Address(DeclarativeBase):
    __tablename__ = 'addresses'
    address_id = Column(Integer,primary_key=True)
    name = Column(Unicode(100))
    address_one = Column(Unicode(100))
    address_two = Column(Unicode(100))
    address_three = Column(Unicode(100))
    city = Column(Unicode(100))
    state = Column(Unicode(100))
    zip_code = Column(Unicode(100))
    phone = Column(Unicode(100))
    fax = Column(Unicode(100))
    contact = Column(Unicode(100))

Michael Bayer

unread,
Jan 7, 2012, 5:06:38 PM1/7/12
to sqlal...@googlegroups.com

On Jan 6, 2012, at 10:15 PM, Paul Kraus wrote:

> I have an address class that i would like to use in several locations for instance vendors need addresses and customers need addresses.
>
> I have my model split between 2 files globals and ar. A customer can have a default bill to address, default ship to address, and multiple address to choose from for ship to's beyond the default. I have the bill_to and ship_to default working fine but for the life of me can't figure out how to create the locations reference. I know how to do it if i put it on the address class but then i have python import issues obviously.

to work around import issues, specify the name of the class to relationship() as a string:

relationship("Address")

This draws upon the class by name from a registry that's represented within the declarative base, and is only resolved when the mappings are first used, assumed to be after all modules have been imported.

docs:

http://www.sqlalchemy.org/docs/orm/relationships.html#one-to-many
http://www.sqlalchemy.org/docs/orm/extensions/declarative.html#configuring-relationships


Reply all
Reply to author
Forward
0 new messages