ok, a lot going on down there...if you want to make a mapper on an class and have it be *the* mapper for that class, do this:

        m = mapper(cls, table, is_primary=True)

Which "clear_mapper()" should also do, but i think its more direct for the new mapper to assert itself as the primary mapper.

What is not going to work so well is taking existing mappers and trying to tinker with their insides...mappers want to be largely immutable (well, completely immutable since they get cached on a constructor-based hash key...but "add_property" breaks that slightly ). youre probably going to want to create brand new mappers for both C1 and C2, especially with the backreference in there.


On Jan 23, 2006, at 10:09 PM, [EMAIL PROTECTED] wrote:

hi,

i'm trying to do something which is
perhaps fundamentally ill-advised,
but it would be great if something
like this could work.

the code is below, but it probably
could use some explanation.

i start with 2 tables 't1' and 't2a',
where 't2a' FKs into 't1'. naturally,
i create a relation between the two,
including a backref, so that 't1'
objects have a 't2s' attribute.

now what i'd like to do is to dynamically
create a new table like 't2a', say 't2b',
which also FKs into 't1', and basically
*replace* the relation that existed
between 't1' and 't2a' with a new one
between 't1' and 't2b'.

i tried a number of things, which are
all included in the code below, such
as manually deleting the property,
recreating the mapper, etc, but it
seems that no matter what i do the
second 'print x1.t2s' prints the
same thing as the first.

any thoughts/advice would be appreciated,
thanks,d

---
import sqlalchemy as rdb

PROP_DEL = False # Do only one of PROP_DEL or CLEAR_MAP
CLEAR_MAP = False

DO_COMMIT = False
REGET = False # Depends on DO_COMMIT

MAKE2B = False # Shouldn't have anything to do with it, but ...

rdb.mapping.objectstore.LOG = True

engine = rdb.create_engine('sqlite://', echo=True)

# Setup
t1 = rdb.Table(
    't1', engine,
    rdb.Column('id', rdb.Integer, primary_key=True)
    )
t1.create()
class C1(object):
    pass
m1 = rdb.mapper(C1, t1)

def create2(n, t, C, m):
    t2 = rdb.Table(
        n, engine,
        rdb.Column('id', rdb.Integer, primary_key=True),
        rdb.Column('t1_id', rdb.Integer, rdb.ForeignKey(t1.c.id)),
        )
    t2.create()
    class C2(object):
        pass
    m2 = rdb.mapper(C2, t2)
    if PROP_DEL:
        try:
            del m.props['t2s']
        except KeyError:
            pass
    if CLEAR_MAP:
        rdb.mapping.clear_mapper(m)
        m = rdb.mapper(C, t)
    m2.add_property(
        't1', rdb.relation(m, backref='t2s'))
    return (t2, C2, m2)

# Create a C1
x1 = C1()
x1.id = 0

# Bind it's 't2s'
(t2a, C2a, m2a) = create2('t2a', t1, C1, m1)

# ... and create some t2 data
N = 5
for i in xrange(N):
    x2a = C2a()
    x2a.t1 = x1

if DO_COMMIT:
    rdb.objectstore.commit()
    rdb.objectstore.clear()

print x1.t2s
print m1.props

# Try re-binding to a new table/object set
(t2b, C2b, m2b) = create2('t2b', t1, C1, m1)
if MAKE2B:
    x2b = C2b()
    x2b.t1 = x1

if DO_COMMIT:
    rdb.objectstore.commit()
    rdb.objectstore.clear()

if REGET:
    del x1
    x1 = m1.selectone()

# Expect x1.t2s to be []
# ... unless we MAKE2B
print x1.t2s
print m1.props



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! http://sel.as-us.falkag.net/sel? cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to