[sqlalchemy] Accessing several databases

2011-06-16 Thread Julian J. M.
Hello,

I'm intending to use sqalchemy with orm for loading and storing my
application's project files. Each sqlite database would be a project
file, that will have several tables.

I'd like to work with projects like this:

project1=AppProject(/tmp/pr1.sqlite);
project2=AppProject(/tmp/pr2.sqlite);

item1 = project1.getItem(5) # item1 should be and object of a mapped
class.
item1.value=test
anotheritem = project1.getNewItem()
anotheritem.value=this is new
# this should flush and commit the underlying session for project1,
#modifying item with id 5, and adding a new one
project1.commitEverything()

item2 = project2.getItem(8)
item2.value = another test
project2.commitEverything()


The problem i'm facing is how to create the engine, metadata, mapper,
session, and the orm classes for each AppProject instance.

I'm not sure if this is supported or even a good idea.

Thanks,
Julian J. M.

-- 
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.



RE: [sqlalchemy] Accessing several databases

2011-06-16 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Julian J. M.
 Sent: 16 June 2011 11:43
 To: sqlalchemy
 Subject: [sqlalchemy] Accessing several databases
 
 Hello,
 
 I'm intending to use sqalchemy with orm for loading and storing my
 application's project files. Each sqlite database would be a project
 file, that will have several tables.
 
 I'd like to work with projects like this:
 
 project1=AppProject(/tmp/pr1.sqlite);
 project2=AppProject(/tmp/pr2.sqlite);
 
 item1 = project1.getItem(5) # item1 should be and object of a mapped
 class.
 item1.value=test
 anotheritem = project1.getNewItem()
 anotheritem.value=this is new
 # this should flush and commit the underlying session for project1,
 #modifying item with id 5, and adding a new one
 project1.commitEverything()
 
 item2 = project2.getItem(8)
 item2.value = another test
 project2.commitEverything()
 
 
 The problem i'm facing is how to create the engine, metadata, mapper,
 session, and the orm classes for each AppProject instance.
 
 I'm not sure if this is supported or even a good idea.
 
 Thanks,
 Julian J. M.
 

I think this should be pretty easy with a separate SQLAlchemy Session
per project. You would define all your mappers and so on without any
reference to a specific database:

##
# your_db_module.py
import sqlalchemy as sa
import sqlalchemy.orm as saorm
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class ProjectItem(Base):
__tablename__ = 'project_item'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)

# other columns etc.



Then your AppProject class would look something like this:

##
# appproject.py
import sqlalchemy as sa
import sqlalchemy.orm as saorm

from your_db_module import ProjectItem

class AppProject(object):
def __init__(self, filename):
self.engine = sa.create_engine('sqlite://' + filename)
self.session = saorm.Session(bind=self.engine)

def get_item(self, id):
return self.session.query(ProjectItem).get(id)



Hope that helps,

Simon

-- 
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.



RE: [sqlalchemy] mapping a class linked with two other classes (AttributeError: 'str' object has no attribute '_sa_instance_state')

2011-06-16 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Jules Stevenson
 Sent: 16 June 2011 08:44
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] mapping a class linked with two other classes
 (AttributeError: 'str' object has no attribute '_sa_instance_state')
 
 Hi List,
 
 I have a user class, a contact class, and a googleID class.
 
 the contact class has can have a googleID per user in the system. I'm
 trying to map it out as follows:
 
 # ArkContact - clientprojectshot module
 orm.mapper(ArkContact, contacts_table, properties={
 'notes': orm.relation(ArkNote,
 secondary=contact_notes_table,
 backref='contacts',
 single_parent=True,
 cascade=all, delete, delete-orphan),
 'users': orm.relation(ArkUser,
 secondary=user_contact_table,
 backref='contacts'),
 'google_UID': orm.relation(ArkUserContactGUID,
 cascade=all, delete,
 backref='user')
 })
 
 #user contact google_GUID
 user_contact_UID = sa.Table('user_contact_UID_table', meta.metadata,
 sa.Column('user_id', sa.types.Integer, sa.ForeignKey('users.id'),
 primary_key=True),
 sa.Column('contact_id', sa.types.Integer,
 sa.ForeignKey('contacts.id'), primary_key=True),
 sa.Column('google_UID', sa.types.String(length = 1024))
 )
 
 class ArkUserContactGUID(object):
 def __init__(self):
 pass
 
 orm.mapper(ArkUserContactGUID, user_contact_UID)
 
 This raises two issues, the first is that an instrumented list is
 returned for the google_UID paramter on the contact object, whereas
 there should only ever be one (since as an operator there is only
 ever
 one user signed in - you).
 

For one-to-one relationships, you should supply uselist=False to your
relationship:

http://www.sqlalchemy.org/docs/orm/relationships.html#one-to-one


 The second is it outright errors :), presumably because my mapping is
 off:
 
 File 'C:\\ark\\ark\\controllers\\contacts.py', line 368 in
 initial_sync
   contact_sync.initial_sync()
 File 'C:\\ark\\ark\\arkTools\\arkGoogle.py', line 121 in initial_sync
   self.add_contact_to_google(contact)
 File 'C:\\ark\\ark\\arkTools\\arkGoogle.py', line 259 in
 add_contact_to_google
   data.google_UID.append(entry.get_id())
 File 'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-
 py2.6.egg\\sqlalchemy\\orm\\collections.py',
 line 952 in append
   item = __set(self, item, _sa_initiator)
 File 'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-
 py2.6.egg\\sqlalchemy\\orm\\collections.py',
 line 927 in __set
   item = getattr(executor, 'fire_append_event')(item, _sa_initiator)
 File 'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-
 py2.6.egg\\sqlalchemy\\orm\\collections.py',
 line 618 in fire_append_event
   item, initiator)
 File 'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-
 py2.6.egg\\sqlalchemy\\orm\\attributes.py',
 line 741 in fire_append_event
   value = fn(state, value, initiator or self)
 File 'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-
 py2.6.egg\\sqlalchemy\\orm\\unitofwork.py',
 line 35 in append
   item_state = attributes.instance_state(item)
 AttributeError: 'str' object has no attribute '_sa_instance_state'
 
 Many thanks for any help!
 
 Jules

You're passing a string (presumably the result of entry.get_id()) where
SA is expecting an instance of a mapped class. I haven't looked at your
mapping in detail, but rather than this:

  data.google_UID.append(entry.get_id())

you probably want something like this:

  obj = ArkUserContactGUID(google_UID=entry.get_id())
  data.google_UID.append(obj)

(If I've misunderstood your mapping, these class names are probably
wrong)

Hope that helps,

Simon

-- 
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.



Re: [sqlalchemy] Accessing several databases

2011-06-16 Thread Julian J. M.
Thank you very much. I'll try that.

I was confused by this howto:
http://turbogears.org/2.1/docs/main/MultipleDatabases.html
They call declarative_base() for each each database... I thought I
needed that for each AppProject instance...

Anyway, thanks agaon for the example. I'll let you know.

Julian.

On Thu, Jun 16, 2011 at 13:54, King Simon-NFHD78
simon.k...@motorolasolutions.com wrote:
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Julian J. M.
 Sent: 16 June 2011 11:43
 To: sqlalchemy
 Subject: [sqlalchemy] Accessing several databases

 Hello,

 I'm intending to use sqalchemy with orm for loading and storing my
 application's project files. Each sqlite database would be a project
 file, that will have several tables.

 I'd like to work with projects like this:

 project1=AppProject(/tmp/pr1.sqlite);
 project2=AppProject(/tmp/pr2.sqlite);

 item1 = project1.getItem(5) # item1 should be and object of a mapped
 class.
 item1.value=test
 anotheritem = project1.getNewItem()
 anotheritem.value=this is new
 # this should flush and commit the underlying session for project1,
 #modifying item with id 5, and adding a new one
 project1.commitEverything()

 item2 = project2.getItem(8)
 item2.value = another test
 project2.commitEverything()


 The problem i'm facing is how to create the engine, metadata, mapper,
 session, and the orm classes for each AppProject instance.

 I'm not sure if this is supported or even a good idea.

 Thanks,
     Julian J. M.


 I think this should be pretty easy with a separate SQLAlchemy Session
 per project. You would define all your mappers and so on without any
 reference to a specific database:

 ##
 # your_db_module.py
 import sqlalchemy as sa
 import sqlalchemy.orm as saorm
 from sqlalchemy.ext.declarative import declarative_base

 Base = declarative_base()

 class ProjectItem(Base):
    __tablename__ = 'project_item'
    id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)

    # other columns etc.



 Then your AppProject class would look something like this:

 ##
 # appproject.py
 import sqlalchemy as sa
 import sqlalchemy.orm as saorm

 from your_db_module import ProjectItem

 class AppProject(object):
    def __init__(self, filename):
        self.engine = sa.create_engine('sqlite://' + filename)
        self.session = saorm.Session(bind=self.engine)

    def get_item(self, id):
        return self.session.query(ProjectItem).get(id)



 Hope that helps,

 Simon

 --
 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.





-- 
http://www.julianmenendez.es

-- 
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.



Re: [sqlalchemy] mapping a class linked with two other classes (AttributeError: 'str' object has no attribute '_sa_instance_state')

2011-06-16 Thread Jules Stevenson
Hi Simon, thank you very much for your help (again!), and yes, that's
how I'm using the mapping :).

I'm now getting an SQL error, I think because the user is not being
pulled automatically through when trying to add the
ArkUserContactGUID:

OperationalError: (OperationalError) (1364, Field 'user_id' doesn't
have a default value) 'INSERT INTO `user_contact_UID_table`
(contact_id, uid) VALUES (%s, %s)' (2L,
'http://www.google.com/m8/feeds/contacts/jules%40kettlestudio.co.uk/base/7c5456c108b2111b')

File 'C:\\ark\\ark\\controllers\\contacts.py', line 368 in initial_sync
  contact_sync.initial_sync()
File 'C:\\ark\\ark\\arkTools\\arkGoogle.py', line 121 in initial_sync
  self.add_contact_to_google(contact)
File 'C:\\ark\\ark\\arkTools\\arkGoogle.py', line 263 in add_contact_to_google
  meta.Session.commit()
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\scoping.py',
line 113 in do
  return getattr(self.registry(), name)(*args, **kwargs)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\session.py',
line 617 in commit
  self.transaction.commit()
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\session.py',
line 293 in commit
  self._prepare_impl()
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\session.py',
line 277 in _prepare_impl
  self.session.flush()
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\session.py',
line 1473 in flush
  self._flush(objects)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\session.py',
line 1542 in _flush
  flush_context.execute()
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\unitofwork.py',
line 327 in execute
  rec.execute(self)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\unitofwork.py',
line 471 in execute
  uow
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\orm\\mapper.py',
line 2163 in _save_obj
  execute(statement, params)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\engine\\base.py',
line 1358 in execute
  params)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\engine\\base.py',
line 1491 in _execute_clauseelement
  compiled_sql, distilled_params
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\engine\\base.py',
line 1599 in _execute_context
  context)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\engine\\base.py',
line 1592 in _execute_context
  context)
File 
'C:\\ark\\env_x64\\lib\\site-packages\\sqlalchemy-0.7.1-py2.6.egg\\sqlalchemy\\engine\\default.py',
line 325 in do_execute
  cursor.execute(statement, parameters)
File 'C:\\ark\\env_x64\\lib\\site-packages\\MySQLdb\\cursors.py', line
173 in execute
  self.errorhandler(self, exc, value)
File 'C:\\ark\\env_x64\\lib\\site-packages\\MySQLdb\\connections.py',
line 36 in defaulterrorhandler
  raise errorclass, errorvalue
OperationalError: (OperationalError) (1364, Field 'user_id' doesn't
have a default value) 'INSERT INTO `user_contact_UID_table`
(contact_id, uid) VALUES (%s, %s)' (2L,
'http://www.google.com/m8/feeds/contacts/jules%40kettlestudio.co.uk/base/7c5456c108b2111b')

I think this is because the contacts object uses a many to many
relationship (a contact can belong to any or all users), code as
follows:

# ArkContact - clientprojectshot module
orm.mapper(ArkContact, contacts_table, properties={
'notes': orm.relation(ArkNote,
secondary=contact_notes_table,
backref='contacts',
single_parent=True,
cascade=all, delete, delete-orphan),
'users': orm.relation(ArkUser,
secondary=user_contact_table,
backref='contacts'),
'google_UID': orm.relation(ArkUserContactGUID,
cascade=all, delete,
uselist=False,
backref='user')
})


I'm thinking I should join between the user and ArkUserContactGUID
somehow, but don't know how...

Many thanks,

Jules

On Thu, Jun 16, 2011 at 2:04 PM, King Simon-NFHD78
simon.k...@motorolasolutions.com wrote:
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Jules Stevenson
 Sent: 16 June 2011 08:44
 To: sqlalchemy@googlegroups.com
 Subject: [sqlalchemy] mapping a class linked with two other classes
 (AttributeError: 'str' object has no attribute '_sa_instance_state')

 Hi List,

 I have a user class, a contact class, and a googleID class.

 the contact class has can have a googleID per user in the system. I'm
 trying to map it out as follows:

 # ArkContact - clientprojectshot module
 orm.mapper(ArkContact, contacts_table, properties={
     'notes': orm.relation(ArkNote,
             

Re: [sqlalchemy] mapping a class linked with two other classes (AttributeError: 'str' object has no attribute '_sa_instance_state')

2011-06-16 Thread Jules Stevenson
Ack, ignore :). There was no direct relationship to the user at all,
doh. So I'm passing that into the ArkUserContactGUID construction, and
all works well.

Thanks again for the help, much appreciated.

Jules

-- 
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.



Re: [sqlalchemy] Accessing several databases

2011-06-16 Thread Michael Bayer
ah good to know, I'm always wondering why people get confused about things like 
this...

On Jun 16, 2011, at 9:33 AM, Julian J. M. wrote:

 Thank you very much. I'll try that.
 
 I was confused by this howto:
 http://turbogears.org/2.1/docs/main/MultipleDatabases.html
 They call declarative_base() for each each database... I thought I
 needed that for each AppProject instance...
 
 Anyway, thanks agaon for the example. I'll let you know.
 
 Julian.
 
 On Thu, Jun 16, 2011 at 13:54, King Simon-NFHD78
 simon.k...@motorolasolutions.com wrote:
 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On Behalf Of Julian J. M.
 Sent: 16 June 2011 11:43
 To: sqlalchemy
 Subject: [sqlalchemy] Accessing several databases
 
 Hello,
 
 I'm intending to use sqalchemy with orm for loading and storing my
 application's project files. Each sqlite database would be a project
 file, that will have several tables.
 
 I'd like to work with projects like this:
 
 project1=AppProject(/tmp/pr1.sqlite);
 project2=AppProject(/tmp/pr2.sqlite);
 
 item1 = project1.getItem(5) # item1 should be and object of a mapped
 class.
 item1.value=test
 anotheritem = project1.getNewItem()
 anotheritem.value=this is new
 # this should flush and commit the underlying session for project1,
 #modifying item with id 5, and adding a new one
 project1.commitEverything()
 
 item2 = project2.getItem(8)
 item2.value = another test
 project2.commitEverything()
 
 
 The problem i'm facing is how to create the engine, metadata, mapper,
 session, and the orm classes for each AppProject instance.
 
 I'm not sure if this is supported or even a good idea.
 
 Thanks,
 Julian J. M.
 
 
 I think this should be pretty easy with a separate SQLAlchemy Session
 per project. You would define all your mappers and so on without any
 reference to a specific database:
 
 ##
 # your_db_module.py
 import sqlalchemy as sa
 import sqlalchemy.orm as saorm
 from sqlalchemy.ext.declarative import declarative_base
 
 Base = declarative_base()
 
 class ProjectItem(Base):
__tablename__ = 'project_item'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
 
# other columns etc.
 
 
 
 Then your AppProject class would look something like this:
 
 ##
 # appproject.py
 import sqlalchemy as sa
 import sqlalchemy.orm as saorm
 
 from your_db_module import ProjectItem
 
 class AppProject(object):
def __init__(self, filename):
self.engine = sa.create_engine('sqlite://' + filename)
self.session = saorm.Session(bind=self.engine)
 
def get_item(self, id):
return self.session.query(ProjectItem).get(id)
 
 
 
 Hope that helps,
 
 Simon
 
 --
 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.
 
 
 
 
 
 -- 
 http://www.julianmenendez.es
 
 -- 
 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 more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Versioning and multi-level inheritance

2011-06-16 Thread JPLaverdure
Hello,

I'm trying to use the versioning recipe describe on the website along
with a multi-level inheritance model (Joined-Table inheritance)

Here are my declarative statements:

[code]
class Sample(Base):
__metaclass__ = VersionedMeta
__tablename__ = 'sample'
__table_args__ = {'schema': 'test'}

id = Column(Integer, primary_key=True)
discriminator = Column('type', String(50))
token = Column(String(128), nullable=False)
source_sample_id = Column(Integer, ForeignKey('test.sample.id'))

children = relationship(Sample, backref=backref('source_sample',
remote_side=id), single_parent=True)

__mapper_args__ = {'polymorphic_on': discriminator,
'polymorphic_identity':'sample'}

def __init__(self, token, source_sample_id=None):
self.token = token
self.source_sample_id = source_sample_id

class Tissue(Sample):
__metaclass__ = VersionedMeta
__tablename__ = 'tissue'
__mapper_args__ = {'polymorphic_identity': 'tissue'}
__table_args__ = {'schema': 'test'}

id = Column(Integer, ForeignKey('test.sample.id'),
primary_key=True)
concentration = Column(String(32))

def __init__(self, token, concentration, source_sample_id=None):
super(Sample, self).__init__(token, source_sample_id)
self.concentration = concentration

class LeukemicTissue(Tissue):
__metaclass__ = VersionedMeta
__tablename__ = 'leukemic_tissue'
__mapper_args__ = {'polymorphic_identity': 'leukemic_tissue'}
__table_args__ = {'schema': 'test'}

id = Column(Integer, ForeignKey('test.tissue.id'),
primary_key=True)
leukemia = Column(String)

def __init__(self, token, concentration, leukemia,
source_sample_id=None):
super(Tissue, self).__init__(token, concentration,
source_sample_id)
self.leukemia = leukemia
[/code]


Whenever I try to create_all() I get the following error:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'tissue_history' and 'leucegene_tissue_history'.

Single level-inheritance works beautifully (ie if I stop at Tissue
and don't declare the LeukemicTissue) but I really need a multi-
level inheritance scheme to work..

Can anyone give me any pointers ?

Thanks !!

-- 
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.



Re: [sqlalchemy] Versioning and multi-level inheritance

2011-06-16 Thread Michael Bayer
apply this patch to history_meta.py

diff -r 7c65c0cdd3c8 examples/versioning/history_meta.py
--- a/examples/versioning/history_meta.py   Tue Jun 14 19:57:21 2011 -0400
+++ b/examples/versioning/history_meta.py   Thu Jun 16 12:04:43 2011 -0400
@@ -35,7 +35,7 @@
 col.unique = False
 
 if super_mapper and col_references_table(column, 
super_mapper.local_table):
-super_fks.append((col.key, 
list(super_history_mapper.base_mapper.local_table.primary_key)[0]))
+super_fks.append((col.key, 
list(super_history_mapper.local_table.primary_key)[0]))
 
 cols.append(col)
 

as you can see, the foreign key generation is pulling from the base mapper 
which isn't the case in a multilevel situation, it needs to pull the FKs from 
the immediate super-mapper.



On Jun 16, 2011, at 11:20 AM, JPLaverdure wrote:

 Hello,
 
 I'm trying to use the versioning recipe describe on the website along
 with a multi-level inheritance model (Joined-Table inheritance)
 
 Here are my declarative statements:
 
 [code]
 class Sample(Base):
__metaclass__ = VersionedMeta
__tablename__ = 'sample'
__table_args__ = {'schema': 'test'}
 
id = Column(Integer, primary_key=True)
discriminator = Column('type', String(50))
token = Column(String(128), nullable=False)
source_sample_id = Column(Integer, ForeignKey('test.sample.id'))
 
children = relationship(Sample, backref=backref('source_sample',
 remote_side=id), single_parent=True)
 
__mapper_args__ = {'polymorphic_on': discriminator,
 'polymorphic_identity':'sample'}
 
def __init__(self, token, source_sample_id=None):
self.token = token
self.source_sample_id = source_sample_id
 
 class Tissue(Sample):
__metaclass__ = VersionedMeta
__tablename__ = 'tissue'
__mapper_args__ = {'polymorphic_identity': 'tissue'}
__table_args__ = {'schema': 'test'}
 
id = Column(Integer, ForeignKey('test.sample.id'),
 primary_key=True)
concentration = Column(String(32))
 
def __init__(self, token, concentration, source_sample_id=None):
super(Sample, self).__init__(token, source_sample_id)
self.concentration = concentration
 
 class LeukemicTissue(Tissue):
__metaclass__ = VersionedMeta
__tablename__ = 'leukemic_tissue'
__mapper_args__ = {'polymorphic_identity': 'leukemic_tissue'}
__table_args__ = {'schema': 'test'}
 
id = Column(Integer, ForeignKey('test.tissue.id'),
 primary_key=True)
leukemia = Column(String)
 
def __init__(self, token, concentration, leukemia,
 source_sample_id=None):
super(Tissue, self).__init__(token, concentration,
 source_sample_id)
self.leukemia = leukemia
 [/code]
 
 
 Whenever I try to create_all() I get the following error:
 sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
 between 'tissue_history' and 'leucegene_tissue_history'.
 
 Single level-inheritance works beautifully (ie if I stop at Tissue
 and don't declare the LeukemicTissue) but I really need a multi-
 level inheritance scheme to work..
 
 Can anyone give me any pointers ?
 
 Thanks !!
 
 -- 
 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 more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.