Re: Re: [sqlalchemy] Re: Composite Pattern

2011-03-31 Thread nickle
OK. I've found the bug. In my UnionCalendar, I called the children,  
calendars, which didn't match the name in the mappers.


It works, and here is a one file cut down version

from sqlalchemy import create_engine
from sqlalchemy import Table, Integer, Column, Boolean, String, MetaData,  
ForeignKey

from sqlalchemy.orm import mapper, sessionmaker, relationship

class Entity (object):

pass

class Calendar (Entity):
'''
Calendar to contain holiday dates
'''

def __init__ (self, code, name):
self.code = code
self.name = name

def isholiday (self, when):
'''
Is when a holiday in the calendar?
'''
pass


class SimpleCalendar (Calendar):
'''
Calendar with name 'name'
'''

def isholiday (self, when):
return False

def __repr__ (self):
res = %s('%s', %s, %s, %s, %s, %s, %s, %s, '%s')\n % \
(
self.__class__.__name__,
self.code,
self.name
)
return res

class UnionCalendar (Calendar):
'''
A union of more than two calendars
'''

def __init__ (self, cals):
Calendar.__init__(self,  .join ([cal.code for cal in cals]),  - .join  
([cal.name for cal in cals]))

self.children = cals

def isholiday (self, when):
for cal in self.children:
if cal.isholiday (when):
return True
return False

def __repr__ (self):
return UnionCalendar (%s)\n % , .join ([cal.code for cal in  
self.children])


engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData()

Session = sessionmaker(bind=engine)
session = Session()

calendars_table = Table \
(
'calendars',
metadata,
Column ('id', Integer, primary_key=True),
Column ('code', String, nullable=False, unique=True),
Column ('name', String),
Column ('calendar_type', String (20), nullable=False)
)

calendar_children_table = Table \
(
'calendar_children',
metadata,
Column ('parent_id', Integer, ForeignKey ('calendars.id')),
Column ('child_id', Integer, ForeignKey ('calendars.id'))
)

calendar_mapper = \
mapper \
(
Calendar,
calendars_table,
polymorphic_on=calendars_table.c.calendar_type,
polymorphic_identity=calendar
)

simple_calendar_mapper = \
mapper \
(
SimpleCalendar,
inherits=calendar_mapper,
polymorphic_identity='simple'
)

union_calendar_mapper = \
mapper \
(
UnionCalendar,
inherits=calendar_mapper,
polymorphic_identity='union',
properties = \
{
'children':relationship \
(
Calendar,
secondary=calendar_children_table,
primaryjoin = calendar_children_table.c.parent_id == calendars_table.c.id,
secondaryjoin = calendar_children_table.c.child_id == calendars_table.c.id
)
}
)

metadata.create_all (engine)

ny = SimpleCalendar(code='NY',name='New York')
lon = SimpleCalendar(code='LON',name='London')
session.add (ny)
session.add (lon)
session.commit()
uc = UnionCalendar ([ny, lon])
print uc
print 'add'
session.add (uc)
session.flush()
session.commit()
print 'done'

for c in uc.children:
print c.code



Thanks for your help Michael.

If you need a generic example for the docs, I'll write it up.



On , Michael Bayer mike...@zzzcomputing.com wrote:



On Mar 31, 2011, at 6:26 AM, Nick Leaton wrote:





 OK, still one problem. It's not saving the association object.







 Here is the code







 ===



 from sqlalchemy import create_engine


 from sqlalchemy import Table, Integer, Column, Boolean, String,  
MetaData, ForeignKey



 from sqlalchemy.orm import mapper, sessionmaker, relationship







 from Entities.Calendar import Calendar



 from Entities.SimpleCalendar import SimpleCalendar



 from Entities.UnionCalendar import UnionCalendar



 from Entities.Holiday import Holiday



 from Entities.SimpleHoliday import SimpleHoliday



 from Entities.RepeatingHoliday import RepeatingHoliday










 ny = SimpleCalendar(code='NY',name='New  
York',holidays=[SimpleHoliday(New Year, 2011,1,1)])


 lon =  
SimpleCalendar(code='LON',name='London',holidays=[SimpleHoliday(New  
Year, 2011,1,1),RepeatingHoliday('MayDay',5,1)])



 session.add (ny)



 session.add (lon)



 session.commit()



 uc = UnionCalendar ([ny, lon])



 print uc



 print 'add'



 session.add (uc)



 session.flush()






So I'd need to see where you are populating children. I don't see that  
in the example here (if it is happening, I assume the constructor of  
UnionCalendar is doing it somehow) so that would be why nothing gets put  
into calendar_children.




if you inline the classes themselves into the test then you'd have an  
example others can reproduce.







--


You received this message because you are subscribed to the Google  
Groups sqlalchemy group.



To post to this group, send email to sqlalchemy@googlegroups.com.


To unsubscribe from this group, send email to  
sqlalchemy+unsubscr...@googlegroups.com.


For more options, visit this group at  
http://groups.google.com/group/sqlalchemy?hl=en.




--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For 

[sqlalchemy] Composite Pattern

2011-03-24 Thread Nickle
I'm trying to implement a Composite Pattern, 
http://en.wikipedia.org/wiki/Composite_pattern

In this case its to represent some holiday calendars. I need to
implement a union calendar that is a collection of other calendars.

So a straightforward hierarchy. Calendar, abstract as the top level
class. SimpleCalendar that inherits from Calendar. Then a union
calendar that inherits from Calendar, but I need to implement
children, a collection of calendars.

So its a self referential hierarchy. The point to note is that one
calendar can appear in more than one union calendar, so its a self
referential many to many. Its certain I need an association table as a
result.

I'm not quite sure how to configure this.

I'm trying something along these lines

calendars_table = Table \
(
'calendars',
metadata,
Column ('id', Integer, primary_key=True),
Column ('code', String, nullable=False, unique=True),
Column ('name', String),
Column ('calendar_type', String (20), nullable=False),
Column ('monday', Boolean),
Column ('tuesday', Boolean),
Column ('wednesday', Boolean),
Column ('thurday', Boolean),
Column ('friday', Boolean),
Column ('saturday', Boolean),
Column ('sunday', Boolean)
)

calendar_children_table = Table \
(
'calendar_children',
metadata,
Column ('parent_id', Integer, ForeignKey ('calendars.id')),
Column ('child_id',  Integer, ForeignKey ('calendars.id'))
)

calendar_mapper = mapper (Calendar, calendars_table,
polymorphic_on=calendars_table.c.calendar_type,
polymorphic_identity=calendar)
simple_calendar_mapper = mapper (SimpleCalendar,
inherits=calendar_mapper,
polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
backref='calendar')})
union_calendar_mapper  = mapper (UnionCalendar,
inherits=calendar_mapper, polymorphic_identity='union')

holidays_table = Table \
(
'holidays',
metadata,
Column ('id',  Integer, primary_key=True),
Column ('holiday_type',String(20),  nullable=False),
Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
Column ('description', String ),
Column ('year',Integer),
Column ('month',   Integer),
Column ('day', Integer)
)

holiday_mapper   = mapper (Holiday,  holidays_table,
polymorphic_on=holidays_table.c.holiday_type,
polymorphic_identity='holiday')
repeating_holiday_mapper = mapper (RepeatingHoliday,
inherits=holiday_mapper, polymorphic_identity='repeating')
simple_holiday_mapper= mapper (SimpleHoliday,
inherits=holiday_mapper, polymorphic_identity='simple')

However, I'm not sure how to map the relationships with the
association table.

Points that I think are relevant are

1. It's only the UnionCalendar that has children.
2. Deleting a UnionCalendar should delete the entries in the
association table, but not the children calendars.
3. Navigation is really only needed from the parent UnionCalendar to
the underlying children calendars. However, having a collection back
the other way might have some uses.
4. For SimpleCalender to its Holidays, I haven't yet implemented the
cascaded delete, but I know how to do this.

Thanks

Nick

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



[sqlalchemy] Re: Composite Pattern

2011-03-24 Thread Nickle
Thanks - I'll try and test tonight.

On Mar 24, 4:29 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Mar 24, 2011, at 4:40 AM, Nickle wrote:









  I'm trying to implement a Composite 
  Pattern,http://en.wikipedia.org/wiki/Composite_pattern

  In this case its to represent some holiday calendars. I need to
  implement a union calendar that is a collection of other calendars.

  So a straightforward hierarchy. Calendar, abstract as the top level
  class. SimpleCalendar that inherits from Calendar. Then a union
  calendar that inherits from Calendar, but I need to implement
  children, a collection of calendars.

  So its a self referential hierarchy. The point to note is that one
  calendar can appear in more than one union calendar, so its a self
  referential many to many. Its certain I need an association table as a
  result.

  I'm not quite sure how to configure this.

  I'm trying something along these lines

  calendars_table = Table \
     (
     'calendars',
     metadata,
     Column ('id', Integer, primary_key=True),
     Column ('code', String, nullable=False, unique=True),
     Column ('name', String),
     Column ('calendar_type', String (20), nullable=False),
     Column ('monday', Boolean),
     Column ('tuesday', Boolean),
     Column ('wednesday', Boolean),
     Column ('thurday', Boolean),
     Column ('friday', Boolean),
     Column ('saturday', Boolean),
     Column ('sunday', Boolean)
     )

  calendar_children_table = Table \
     (
     'calendar_children',
     metadata,
     Column ('parent_id', Integer, ForeignKey ('calendars.id')),
     Column ('child_id',  Integer, ForeignKey ('calendars.id'))
     )

  calendar_mapper = mapper (Calendar, calendars_table,
  polymorphic_on=calendars_table.c.calendar_type,
  polymorphic_identity=calendar)
  simple_calendar_mapper = mapper (SimpleCalendar,
  inherits=calendar_mapper,
  polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
  backref='calendar')})
  union_calendar_mapper  = mapper (UnionCalendar,
  inherits=calendar_mapper, polymorphic_identity='union')

  holidays_table = Table \
     (
     'holidays',
     metadata,
     Column ('id',          Integer, primary_key=True),
     Column ('holiday_type',        String(20),  nullable=False),
     Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
     Column ('description', String ),
     Column ('year',        Integer),
     Column ('month',       Integer),
     Column ('day',         Integer)
     )

  holiday_mapper           = mapper (Holiday,          holidays_table,
  polymorphic_on=holidays_table.c.holiday_type,
  polymorphic_identity='holiday')
  repeating_holiday_mapper = mapper (RepeatingHoliday,
  inherits=holiday_mapper, polymorphic_identity='repeating')
  simple_holiday_mapper    = mapper (SimpleHoliday,
  inherits=holiday_mapper, polymorphic_identity='simple')

  However, I'm not sure how to map the relationships with the
  association table.

  Points that I think are relevant are

  1. It's only the UnionCalendar that has children.

 apply a relatlonship() to the UnionCalendar mapping, the target class is 
 Calendar, secondary is calendar_children_table.

  2. Deleting a UnionCalendar should delete the entries in the
  association table, but not the children calendars.

 This is a given when using secondary with relationship.   Severing the link 
 between parent and child corresponds to a deletion of the row in secondary.

  3. Navigation is really only needed from the parent UnionCalendar to
  the underlying children calendars. However, having a collection back
  the other way might have some uses.

 just add backref='parent_containers' or similar to the relationship().







  4. For SimpleCalender to its Holidays, I haven't yet implemented the
  cascaded delete, but I know how to do this.

  Thanks

  Nick

  --
  You received this message because you are subscribed to the Google Groups 
  sqlalchemy group.
  To post to this group, send email to sqlalchemy@googlegroups.com.
  To unsubscribe from this group, send email to 
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group 
  athttp://groups.google.com/group/sqlalchemy?hl=en.

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



[sqlalchemy] Re: Composite Pattern

2011-03-24 Thread Nickle
Doesn't quite work.

What I think is the problem is that when using secondary on the
association table, there are two foreign keys both referencing a
calendar. It doesn't know which to pick.

union mapper looks like this

union_calendar_mapper  = \
mapper \
(
UnionCalendar,
inherits=calendar_mapper,
polymorphic_identity='union',
properties =
{
'children':relationship(Calendar,
secondary='calendar_children_table')
}
)


On Mar 24, 4:29 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Mar 24, 2011, at 4:40 AM, Nickle wrote:









  I'm trying to implement a Composite 
  Pattern,http://en.wikipedia.org/wiki/Composite_pattern

  In this case its to represent some holiday calendars. I need to
  implement a union calendar that is a collection of other calendars.

  So a straightforward hierarchy. Calendar, abstract as the top level
  class. SimpleCalendar that inherits from Calendar. Then a union
  calendar that inherits from Calendar, but I need to implement
  children, a collection of calendars.

  So its a self referential hierarchy. The point to note is that one
  calendar can appear in more than one union calendar, so its a self
  referential many to many. Its certain I need an association table as a
  result.

  I'm not quite sure how to configure this.

  I'm trying something along these lines

  calendars_table = Table \
     (
     'calendars',
     metadata,
     Column ('id', Integer, primary_key=True),
     Column ('code', String, nullable=False, unique=True),
     Column ('name', String),
     Column ('calendar_type', String (20), nullable=False),
     Column ('monday', Boolean),
     Column ('tuesday', Boolean),
     Column ('wednesday', Boolean),
     Column ('thurday', Boolean),
     Column ('friday', Boolean),
     Column ('saturday', Boolean),
     Column ('sunday', Boolean)
     )

  calendar_children_table = Table \
     (
     'calendar_children',
     metadata,
     Column ('parent_id', Integer, ForeignKey ('calendars.id')),
     Column ('child_id',  Integer, ForeignKey ('calendars.id'))
     )

  calendar_mapper = mapper (Calendar, calendars_table,
  polymorphic_on=calendars_table.c.calendar_type,
  polymorphic_identity=calendar)
  simple_calendar_mapper = mapper (SimpleCalendar,
  inherits=calendar_mapper,
  polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
  backref='calendar')})
  union_calendar_mapper  = mapper (UnionCalendar,
  inherits=calendar_mapper, polymorphic_identity='union')

  holidays_table = Table \
     (
     'holidays',
     metadata,
     Column ('id',          Integer, primary_key=True),
     Column ('holiday_type',        String(20),  nullable=False),
     Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
     Column ('description', String ),
     Column ('year',        Integer),
     Column ('month',       Integer),
     Column ('day',         Integer)
     )

  holiday_mapper           = mapper (Holiday,          holidays_table,
  polymorphic_on=holidays_table.c.holiday_type,
  polymorphic_identity='holiday')
  repeating_holiday_mapper = mapper (RepeatingHoliday,
  inherits=holiday_mapper, polymorphic_identity='repeating')
  simple_holiday_mapper    = mapper (SimpleHoliday,
  inherits=holiday_mapper, polymorphic_identity='simple')

  However, I'm not sure how to map the relationships with the
  association table.

  Points that I think are relevant are

  1. It's only the UnionCalendar that has children.

 apply a relatlonship() to the UnionCalendar mapping, the target class is 
 Calendar, secondary is calendar_children_table.

  2. Deleting a UnionCalendar should delete the entries in the
  association table, but not the children calendars.

 This is a given when using secondary with relationship.   Severing the link 
 between parent and child corresponds to a deletion of the row in secondary.

  3. Navigation is really only needed from the parent UnionCalendar to
  the underlying children calendars. However, having a collection back
  the other way might have some uses.

 just add backref='parent_containers' or similar to the relationship().







  4. For SimpleCalender to its Holidays, I haven't yet implemented the
  cascaded delete, but I know how to do this.

  Thanks

  Nick

  --
  You received this message because you are subscribed to the Google Groups 
  sqlalchemy group.
  To post to this group, send email to sqlalchemy@googlegroups.com.
  To unsubscribe from this group, send email to 
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group 
  athttp://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http

[sqlalchemy] Re: Composite Pattern

2011-03-24 Thread Nickle
Thanks - sort knew what was needed - didn't know how to express it.

Nick

On Mar 24, 10:20 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Mar 24, 2011, at 5:08 PM, Nickle wrote:









  Doesn't quite work.

  What I think is the problem is that when using secondary on the
  association table, there are two foreign keys both referencing a
  calendar. It doesn't know which to pick.

  union mapper looks like this

  union_calendar_mapper  = \
     mapper \
         (
         UnionCalendar,
         inherits=calendar_mapper,
         polymorphic_identity='union',
         properties =
             {
             'children':relationship(Calendar,
  secondary='calendar_children_table')
             }
         )

 oh thats correct, so primaryjoin and secondaryjoin clear that up, also 
 you pass the Table object itself to secondary without the string:

 relationship(Calendar, secondary=calendar_children_table,
                      
 primaryjoin=calendar_children_table.c.parent_id=calendars_table.c.id,
                      
 secondaryjoin=calendar_children_table.c.child_id=calendars_table.c.id
 )

 so it knows UnionCalendar id 12 linked to Calendar id 17, put the 12 in 
 parent_id, put the 17 in child_id.   Similarly it knows which side is 
 which inside a query(UnionCalendar).join(children).









  On Mar 24, 4:29 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  On Mar 24, 2011, at 4:40 AM, Nickle wrote:

  I'm trying to implement a Composite 
  Pattern,http://en.wikipedia.org/wiki/Composite_pattern

  In this case its to represent some holiday calendars. I need to
  implement a union calendar that is a collection of other calendars.

  So a straightforward hierarchy. Calendar, abstract as the top level
  class. SimpleCalendar that inherits from Calendar. Then a union
  calendar that inherits from Calendar, but I need to implement
  children, a collection of calendars.

  So its a self referential hierarchy. The point to note is that one
  calendar can appear in more than one union calendar, so its a self
  referential many to many. Its certain I need an association table as a
  result.

  I'm not quite sure how to configure this.

  I'm trying something along these lines

  calendars_table = Table \
     (
     'calendars',
     metadata,
     Column ('id', Integer, primary_key=True),
     Column ('code', String, nullable=False, unique=True),
     Column ('name', String),
     Column ('calendar_type', String (20), nullable=False),
     Column ('monday', Boolean),
     Column ('tuesday', Boolean),
     Column ('wednesday', Boolean),
     Column ('thurday', Boolean),
     Column ('friday', Boolean),
     Column ('saturday', Boolean),
     Column ('sunday', Boolean)
     )

  calendar_children_table = Table \
     (
     'calendar_children',
     metadata,
     Column ('parent_id', Integer, ForeignKey ('calendars.id')),
     Column ('child_id',  Integer, ForeignKey ('calendars.id'))
     )

  calendar_mapper = mapper (Calendar, calendars_table,
  polymorphic_on=calendars_table.c.calendar_type,
  polymorphic_identity=calendar)
  simple_calendar_mapper = mapper (SimpleCalendar,
  inherits=calendar_mapper,
  polymorphic_identity='simple',properties={'holidays':relationship(Holiday,
  backref='calendar')})
  union_calendar_mapper  = mapper (UnionCalendar,
  inherits=calendar_mapper, polymorphic_identity='union')

  holidays_table = Table \
     (
     'holidays',
     metadata,
     Column ('id',          Integer, primary_key=True),
     Column ('holiday_type',        String(20),  nullable=False),
     Column ('calendar_id', Integer, ForeignKey ('calendars.id')),
     Column ('description', String ),
     Column ('year',        Integer),
     Column ('month',       Integer),
     Column ('day',         Integer)
     )

  holiday_mapper           = mapper (Holiday,          holidays_table,
  polymorphic_on=holidays_table.c.holiday_type,
  polymorphic_identity='holiday')
  repeating_holiday_mapper = mapper (RepeatingHoliday,
  inherits=holiday_mapper, polymorphic_identity='repeating')
  simple_holiday_mapper    = mapper (SimpleHoliday,
  inherits=holiday_mapper, polymorphic_identity='simple')

  However, I'm not sure how to map the relationships with the
  association table.

  Points that I think are relevant are

  1. It's only the UnionCalendar that has children.

  apply a relatlonship() to the UnionCalendar mapping, the target class is 
  Calendar, secondary is calendar_children_table.

  2. Deleting a UnionCalendar should delete the entries in the
  association table, but not the children calendars.

  This is a given when using secondary with relationship.   Severing the 
  link between parent and child corresponds to a deletion of the row in 
  secondary.

  3. Navigation is really only needed from the parent UnionCalendar to
  the underlying children calendars. However, having a collection back
  the other way might have some uses.

  just add backref='parent_containers' or similar

[sqlalchemy] SQLite and Dates

2011-02-28 Thread Nickle
I have the following code. Simple Calendar and list of holidays. If I
try and use a Date and datetime.date  I get an error as follows

Traceback (most recent call last):
  File H:\workspace\Test\src\stest.py, line 78, in module
lon.holidays = [Holiday(lon,
holiday=datetime.date('2011-01-01'),description='New Year'),
Holiday(lon, holiday=datetime.date('2011-12-25'),description=xmas)]
TypeError: an integer is required

If I change it to use a string/String for the date, its fine.

I know there is an issue with Date and Sqlite but I can't fix it.

The String version is commented out in the holiday_tables code, and it
works

Any help appreciated

=
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Date , Integer, String,
MetaData, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import mapper
from sqlalchemy.orm import sessionmaker
import datetime

engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()

metadata = MetaData()

class Calendar (object):

def __init__(self, name, fullname):
self.name = name
self.fullname = fullname

def __repr__(self):
return Calendar('%s','%s') % (self.name, self.fullname)

class Holiday (object):

def __init__(self, holiday, description):
self.holiday = holiday
self.description = description

def __repr__(self):
return Holiday('%s', '%s') % (self.holiday,
self.description)

calendars_table = Table \
(
'calendars',
metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
Column('fullname', String)
)

holidays_table = Table \
(
'holidays',
metadata,
Column('id', Integer, primary_key=True),
#Column('holiday', String, nullable=False),
Column('holiday', Date, nullable=False),
Column('description', String),
Column('calendar_id', Integer, ForeignKey('calendars.id'))
)

metadata.create_all(engine)

mapper \
(
Calendar,
calendars_table,
properties= \
{
'holidays': relationship \
(
Holiday,
backref='calendar',
cascade=all, delete, delete-orphan,
lazy='joined'
)
}
)

mapper(Holiday, holidays_table)

session.commit()
lon = Calendar('LON', 'London')
#lon.holidays = [Holiday(holiday='2011-01-01',description='New Year'),
Holiday(holiday='2011-12-25',description=xmas)]
session.add(lon)
session.commit()
lon.holidays = [Holiday(lon,
holiday=datetime.date('2011-01-01'),description='New Year'),
Holiday(lon, holiday=datetime.date('2011-12-25'),description=xmas)]
session.commit()
lon = session.query(Calendar).filter_by(name='LON').one()
session.delete(lon)
session.commit()
=

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



[sqlalchemy] Idempotent updating (Update or insert)

2010-11-25 Thread Nickle
I'm trying to get my head around idempotent updating.

ie I'm creating some objects, I want to store them. These objects
might or might not be already stored in the database. If they are, I
want to update them if necessary  (a field has changed), or insert
them if they do not exist.

What's the preferred approach?

Nick

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



[sqlalchemy] Relationships in an inheritance heirarchy

2010-04-19 Thread Nickle
I have the following python classes.

class Instrument (object):

def __init__ (self, name):
self.name = name

class Equity (Instrument):

def __init__ (self, name, currency_id):
Instrument.__init__(self, name)
self.currency_id = currency_id

class Currency (Instrument):
pass

Pretty simple cut down example. Equity has a relationship with a
currency

Mapping is as follows

instruments = Table ('instruments', metadata,
Column ('id', Integer, primary_key=True),
Column ('type', String (10), nullable=False),
Column ('name', String(50), nullable=False, unique=True),
Column ('currency_id', Integer, ForeignKey ('instruments.id'))
)

instrument_mapper   = mapper (Instrument, instruments,
polymorphic_on=instruments.c.type, polymorphic_identity='instrument')
equity_mapper   = mapper (Equity,
inherits=instrument_mapper, polymorphic_identity='equity')
currency_mapper = mapper (Currency,
inherits=instrument_mapper, polymorphic_identity='currency',
  properties={'equities': relationship
(Instrument, backref=backref('currency',
remote_side=[instruments.c.id]))}
  )


One thing I'm not sure about. The currency mapper should have a
relationship with Currency and not with Instruments. ie. I want to
restrict the relationship to Currencies.

Is there a standard way of going around this?

Nick

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