Re: [sqlalchemy] table inheritance: how to change a record from base type to derived type?

2013-12-11 Thread Iain Duncan
On Tue, Dec 10, 2013 at 3:54 PM, Michael Bayer mike...@zzzcomputing.comwrote:


 On Dec 10, 2013, at 5:04 PM, Iain Duncan iainduncanli...@gmail.com
 wrote:

 Hi, I'm stuck on how to do something that I'm sure must be possible.

 I have two kinds of records, using table inheritance, Client, and
 SpecialClient. SpecialClient has extra fields and it's own polymorphic
 identity. I need to *promote* a and existing client record to a special
 client. I tried just making a SpecialClient and copying over attributes,
 but this is giving me can't set attribute error messages. Can anyone tell
 me what the *right* way to do change a polymorphic type, while keeping it's
 ID the same, would be? In my case, the ID in the derived type is an fkey to
 the base type table's id col.


 if its joined inheritance, you have to INSERT into the new table manually,
 that feature isn’t supported right now.   if it’s single, try emitting an
 UPDATE for the “discriminator” column, then reloading the object.


I think it's joined inheritance? The derived table's primarky key is an
id column that is also an fkey to the base table's id column and the
 base table has a _type column holding the poly id. Does this mean I
should drop into sql directly to create the record in the derived table and
it will just work?

thanks Michael!

iain






 Thanks!
 Iain

 --
 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 sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.




-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] table inheritance: how to change a record from base type to derived type?

2013-12-11 Thread Michael Bayer

On Dec 11, 2013, at 12:45 PM, Iain Duncan iainduncanli...@gmail.com wrote:

 On Tue, Dec 10, 2013 at 3:54 PM, Michael Bayer mike...@zzzcomputing.com 
 wrote:
 
 On Dec 10, 2013, at 5:04 PM, Iain Duncan iainduncanli...@gmail.com wrote:
 
 Hi, I'm stuck on how to do something that I'm sure must be possible.
 
 I have two kinds of records, using table inheritance, Client, and 
 SpecialClient. SpecialClient has extra fields and it's own polymorphic 
 identity. I need to *promote* a and existing client record to a special 
 client. I tried just making a SpecialClient and copying over attributes, but 
 this is giving me can't set attribute error messages. Can anyone tell me 
 what the *right* way to do change a polymorphic type, while keeping it's ID 
 the same, would be? In my case, the ID in the derived type is an fkey to the 
 base type table's id col.
 
 if its joined inheritance, you have to INSERT into the new table manually, 
 that feature isn’t supported right now.   if it’s single, try emitting an 
 UPDATE for the “discriminator” column, then reloading the object.
 
 
 I think it's joined inheritance? The derived table's primarky key is an id 
 column that is also an fkey to the base table's id column and the  base 
 table has a _type column holding the poly id. Does this mean I should drop 
 into sql directly to create the record in the derived table and it will just 
 work”?


you need to manipulate the tables directly, and then totally reload the 
original object fresh.  pretty much make the ORM think the previous Client 
never existed.




signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] table inheritance: how to change a record from base type to derived type?

2013-12-10 Thread Iain Duncan
Hi, I'm stuck on how to do something that I'm sure must be possible.

I have two kinds of records, using table inheritance, Client, and
SpecialClient. SpecialClient has extra fields and it's own polymorphic
identity. I need to *promote* a and existing client record to a special
client. I tried just making a SpecialClient and copying over attributes,
but this is giving me can't set attribute error messages. Can anyone tell
me what the *right* way to do change a polymorphic type, while keeping it's
ID the same, would be? In my case, the ID in the derived type is an fkey to
the base type table's id col.

Thanks!
Iain

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] table inheritance: how to change a record from base type to derived type?

2013-12-10 Thread Michael Bayer

On Dec 10, 2013, at 5:04 PM, Iain Duncan iainduncanli...@gmail.com wrote:

 Hi, I'm stuck on how to do something that I'm sure must be possible.
 
 I have two kinds of records, using table inheritance, Client, and 
 SpecialClient. SpecialClient has extra fields and it's own polymorphic 
 identity. I need to *promote* a and existing client record to a special 
 client. I tried just making a SpecialClient and copying over attributes, but 
 this is giving me can't set attribute error messages. Can anyone tell me 
 what the *right* way to do change a polymorphic type, while keeping it's ID 
 the same, would be? In my case, the ID in the derived type is an fkey to the 
 base type table's id col.

if its joined inheritance, you have to INSERT into the new table manually, that 
feature isn’t supported right now.   if it’s single, try emitting an UPDATE for 
the “discriminator” column, then reloading the object.




 
 Thanks!
 Iain
 
 -- 
 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 sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] table inheritance

2010-12-07 Thread Tarek Ziadé
Hey,

I have a declarative table called 'Foo':

_Base = declarative_base()

class Foo(_Base):
__tablename__ = 'foo'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)

Now I create Foo1 that has exactly the same definition but differs
just by the name:

class Foo1(_Base):
__tablename__ = 'foo1'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)


And I need more of those (FooN with N = 10). The use case is to shard
data across several tables.

To avoid cut/n/pastes, I tried inheritance,  meta-class, a type
factory and the like, but everything fails. Whether because I was
reusing the same column class objects or whether because I was
conflicting with SQLALchemy meta-class magic.

Is there any sane way to do this and avoid a crazy copy/paste ?

Cheers
Tarek

-- 
Tarek Ziadé | http://ziade.org

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



Re: [sqlalchemy] table inheritance

2010-12-07 Thread Michael Bayer
This is the entity name recipe, and we have a classical and declarative 
version over at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName .   
 It's using just the straight type() factory. 

You definitely want to use distinct Column objects for each class, this because 
a Column object is immediately made to reference its parent Table, for obvious 
reasons.   Recipes that generate multiple tables from a single set of Column 
objects use the copy() method on Column for this purpose.   However, you don't 
even need to deal with that, since declarative mixins take care of the copy() 
stuff for you.  I've updated the EntityName example to illustrate taking 
advantage of the mixin so check it out.




On Dec 7, 2010, at 10:45 AM, Tarek Ziadé wrote:

 Hey,
 
 I have a declarative table called 'Foo':
 
 _Base = declarative_base()
 
 class Foo(_Base):
__tablename__ = 'foo'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)
 
 Now I create Foo1 that has exactly the same definition but differs
 just by the name:
 
 class Foo1(_Base):
__tablename__ = 'foo1'
__table_args__ = {'mysql_engine': 'InnoDB',
  'mysql_charset': 'latin1'}
id = Column(String(64), primary_key=True, autoincrement=False)
stuff = Column(Integer(11), primary_key=True, nullable=False)
 
 
 And I need more of those (FooN with N = 10). The use case is to shard
 data across several tables.
 
 To avoid cut/n/pastes, I tried inheritance,  meta-class, a type
 factory and the like, but everything fails. Whether because I was
 reusing the same column class objects or whether because I was
 conflicting with SQLALchemy meta-class magic.
 
 Is there any sane way to do this and avoid a crazy copy/paste ?
 
 Cheers
 Tarek
 
 -- 
 Tarek Ziadé | http://ziade.org
 
 -- 
 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.
 

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



Re: [sqlalchemy] table inheritance

2010-12-07 Thread Tarek Ziadé
On Tue, Dec 7, 2010 at 5:08 PM, Michael Bayer mike...@zzzcomputing.com wrote:
 This is the entity name recipe, and we have a classical and declarative 
 version over at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName . 
    It's using just the straight type() factory.

 You definitely want to use distinct Column objects for each class, this 
 because a Column object is immediately made to reference its parent Table, 
 for obvious reasons.   Recipes that generate multiple tables from a single 
 set of Column objects use the copy() method on Column for this purpose.   
 However, you don't even need to deal with that, since declarative mixins take 
 care of the copy() stuff for you.  I've updated the EntityName example to 
 illustrate taking advantage of the mixin so check it out.

Thanks !

I am always amazed by how fast and accurately you are always answering
here, kudos for this.


Cheers
Tarek
-- 
Tarek Ziadé | http://ziade.org

-- 
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] Table Inheritance

2010-11-05 Thread Mark Erbaugh
Please refer to the Joined Table Inheritance Section under declarative.ext (I'm 
using SA 0.5.8).

Is is possible to create a Person who is both an Engineer and a Manager using 
joined table inheritance? IOW, both Manager and Engineer would link to the same 
row in Person.

Thanks,
Mark

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