Hello,
I'm trying to introduce database persistence into an already existent class
hierarchy. That mean I have in a separate module all the class representing
the models of my application (without SQL interaction methods), then I have
in another module the SQL schema and the mapping (so I'm using the
classical mapping):
*sql.py*
*import sqlalchemy.orm as ormfrom sqlalchemy import Metadata, Integer,
String, DateTimefrom myproject.models import Item, Ordermetadata =
MetaData()item = Table('item', metadata, Column('id', Integer,
primary_key=True), Column('name', String),
Column('quantity', Integer) )order = Order('order', metadata,
Column('id', Integer, primary_key=True),
Column('date', DateTime), )orm.mapper(Item,
item)orm.mapper(Order, order)*
*models.py*
class Item(object):
def __init__(self, name, quantity):
self.name = name
self.quantity = quantity
class Order(object):
def __init__(self, date):
self.date = date
Now I would like to introduce a M2M relationship with additional column in
the association table:
order_item = Table('order_item', metadata,
Column('order_id', Integer, ForeignKey('order.id')),
Column('item_id', Integer, ForeignKey('item.id')),
Column('price', Float)
)
In the documentation, when they present the Association proxy, they do that
in a declarative way. So they add an attribute to the class:
# ... Class definition ...
items = association_proxy('order_items', 'items')
Is there a way to achieve this without touching the classes in *models.py?
*Maybe
using the *orm.mapper? *Or the only way is to modify the class adding the
association proxy like in the documentation? Do you have any reference to
point me out on the right direction to use association proxies with
classical mapping?
Thanks,
PS: You find this same question on SO, if you want you can answer also there
<https://stackoverflow.com/q/45259925/1334711>.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.