[sqlalchemy] Re: Get default value

2008-01-11 Thread Alexandre da Silva


Em Sex, 2008-01-11 às 08:54 +0200, [EMAIL PROTECTED] escreveu:

 either put  the correct polymorphic_on=resource.c.poly, or remove it 
 alltogether,
 it comes from the inherited base-mapper.

Exactly, works fine leaving poly on resource and mapping all  
polymorphic_on=resource.c.poly

cookbook helps me to, but just this change make my model working

Thank you a lot, and thank's to all replies.

follow the correct mapping to anyone wants:

-code---8--code-

from sqlalchemy import create_engine, MetaData, Table, Column, types, ForeignKey
from sqlalchemy.orm import mapper, relation, backref, create_session
from sqlalchemy.sql.expression import outerjoin, join
from sqlalchemy import String, Unicode, Integer, DateTime, Numeric, Boolean, 
UnicodeText


db = create_engine('sqlite:///sa.db')

metadata = MetaData()
metadata = MetaData(db)
metadata.bind = db
session = create_session(bind=db)


resource_table = Table('resource', metadata,
Column('id',Integer, primary_key=True),
Column('name', String(30)),
Column('poly', String(31), nullable=True)
)

person_table = Table('person', metadata,
Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
)

material_table = Table('material', metadata,
Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
)

employee_table = Table('employee', metadata,
Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

technical_table = Table('technical', metadata,
Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

class Resource(object):
def __init__(self, name):
self.name = name

def __repr__(self):
return Resource  id=%d ,name=%s  % (self.id,self.name)

class Person(Resource):
def __repr__(self):
return Person  id=%d ,name=%s  % (self.id,self.name)

class Material(Resource):
def __repr__(self):
return Material  id=%d ,name=%s  % (self.id,self.name)

class Employee(Person):
def __repr__(self):
return Employee  id=%d ,name=%s  % (self.id,self.name)

class Technical(Person):
def __repr__(self):
return Technical  id=%d ,name=%s  % (self.id,self.name)

mapper(Resource, resource_table,
polymorphic_on=resource_table.c.poly,
polymorphic_identity='resource',
)

mapper(Person, person_table, 
inherits=Resource, polymorphic_identity='person',
polymorphic_on= resource_table.c.poly, 
)

mapper(Material, material_table, 
inherit_condition= material_table.c.id == resource_table.c.id, 
inherits=Resource, polymorphic_identity='material'
)

mapper(Employee, employee_table,
inherit_condition= employee_table.c.id == person_table.c.id, 
inherits=Person, polymorphic_identity='employee',
)

mapper(Technical, technical_table,
inherit_condition= technical_table.c.id == person_table.c.id, 
inherits=Person, polymorphic_identity='technical',
)

metadata.create_all(bind=db)


r = Resource('resource name')
p = Person('person name')
m = Material('material name')
e = Employee('employee name')
t = Technical('technical name')

session.save(r)
session.save(p)
session.save(m)
session.save(e)
session.save(t)

session.flush()
session.clear()

print  LIST FROM RESOURCES #

for o in session.query(Resource).all():
print o, o.poly

print  LIST FROM PERSONS #

for o in session.query(Person).all():
print o, o.poly

-code---8--code-

results are:

 LIST FROM RESOURCES #
Resource  id=1 ,name=resource name  resource
Person  id=2 ,name=person name  person
Material  id=3 ,name=material name  material
Employee  id=4 ,name=employee name  employee
Technical  id=5 ,name=technical name  technical
 LIST FROM PERSONS #
Person  id=2 ,name=person name  person
Employee  id=4 ,name=employee name  employee
Technical  id=5 ,name=technical name  technical

Thanks again,

Att

-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Rick Morrison
Isn't it just

   column.default ?

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Rick Morrison
Ah, I read too fast, you are getting back the ColumnDefault object

   try column.default.arg

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread jason kirtland

Alexandre da Silva wrote:
 Hello all,
 
 is there a way that I can get the default value defined on
 Column(default=something) ?
 
 
 the default I get is a ColumDefault, and I don't know how to get it's
 value.
 
 any suggestion?

The value is in its .arg property:  col.default.arg



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Alexandre da Silva


Em Qui, 2008-01-10 às 17:13 -0800, jason kirtland escreveu:
 col.default.arg

Yes, exactly this.

another question

How many levels I can inherit classes/tables without get something
wrong?

let me show a simplest sample hierarchy:

resource
  / \
person   material
 /  \
 employeecustomer


now, I am creating a type column on resource to map persons and
materials, them I am creating another type column on person, to get
mapped the various types of persons.

by this way, I could get a employee instance, just selecting a resource
but something is going wrong, because the column type on resource is not
filled with correct value, and I am getting NOT NULL CONSTRAINT by
insert an employee.


the tables

resource_table = Table(
Column('id',Integer, primary_key=True),
Column('poly', String(31), nullable=False)
)

person_table = Table(
Column('id',Integer, primary_key=True, ForeignKey('resource.id'),
primary_key=True)),
Column('poly', String(31), nullable=False)
)

employee_table = Table(
Column('id',Integer, primary_key=True, ForeignKey('person.id'),
primary_key=True)),
)

the classes

class Resource(object):
pass

class Person(Resource):
pass

class Employee(Person):
pass


mappers

mapper(Resource, resource_table,
polymorphic_on=resource_table.c.poly,
polymorphic_identity='resource'
)

mapper(Person, person_table, 
polymorphic_on=person_table.c.poly,
inherits=Resource, polymorphic_identity='person'
)


mapper(Employee employee_table,
inherits=Person, polymorphic_identity='employee',
)


is all, now when I create an instance of Employee and try to save, I get
back an integrity error, that resource.poly cannot be null

any suggestion?

thank's for previous replies.


-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Rick Morrison
You're mixing single-table inheritance (using the discriminator column),
with concrete inheritance (using multiple tables).

You have to pick one scheme or the other. Either use a single inheritance
chain, or separate the two class hierarchies into two separate chains that
don't inherit from each other. In the separate scheme, each chain can use
it's own discriminator column, but you cannot combine two class hierarchies
that each use a different discriminator column.


On Jan 10, 2008 8:46 PM, Alexandre da Silva [EMAIL PROTECTED] wrote:



 Em Qui, 2008-01-10 às 17:13 -0800, jason kirtland escreveu:
  col.default.arg

 Yes, exactly this.

 another question

 How many levels I can inherit classes/tables without get something
 wrong?

 let me show a simplest sample hierarchy:

resource
  / \
person   material
 /  \
 employeecustomer


 now, I am creating a type column on resource to map persons and
 materials, them I am creating another type column on person, to get
 mapped the various types of persons.

 by this way, I could get a employee instance, just selecting a resource
 but something is going wrong, because the column type on resource is not
 filled with correct value, and I am getting NOT NULL CONSTRAINT by
 insert an employee.


 the tables

 resource_table = Table(
Column('id',Integer, primary_key=True),
Column('poly', String(31), nullable=False)
 )

 person_table = Table(
Column('id',Integer, primary_key=True, ForeignKey('resource.id'),
 primary_key=True)),
Column('poly', String(31), nullable=False)
 )

 employee_table = Table(
Column('id',Integer, primary_key=True, ForeignKey('person.id'),
 primary_key=True)),
 )

 the classes

 class Resource(object):
pass

 class Person(Resource):
pass

 class Employee(Person):
pass


 mappers

 mapper(Resource, resource_table,
polymorphic_on=resource_table.c.poly,
polymorphic_identity='resource'
)

 mapper(Person, person_table,
polymorphic_on=person_table.c.poly,
inherits=Resource, polymorphic_identity='person'
)


 mapper(Employee employee_table,
inherits=Person, polymorphic_identity='employee',
)


 is all, now when I create an instance of Employee and try to save, I get
 back an integrity error, that resource.poly cannot be null

 any suggestion?

 thank's for previous replies.


 --
 Alexandre da Silva
 Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Alexandre da Silva


Em Qui, 2008-01-10 às 21:20 -0500, Rick Morrison escreveu:
 You're mixing single-table inheritance (using the discriminator
 column), with concrete inheritance (using multiple tables). 
 
 You have to pick one scheme or the other. Either use a single
 inheritance chain, or separate the two class hierarchies into two
 separate chains that don't inherit from each other. In the separate
 scheme, each chain can use it's own discriminator column, but you
 cannot combine two class hierarchies that each use a different
 discriminator column. 

hum, so something is wrong here, I recreate a simple test case and all
is working properly, using the exactly model suggested on my previous
message. I will debug my application to find where is the error, but for
now, I don't know if this should work, but is working.

here is the test case and results:

code-8--code--

from sqlalchemy import create_engine, MetaData, Table, Column, types,
ForeignKey
from sqlalchemy.orm import mapper, relation, backref, create_session
from sqlalchemy import String, Unicode, Integer, DateTime, Numeric,
Boolean, UnicodeText


db = create_engine('sqlite:///:memory:')

metadata = MetaData()
metadata = MetaData(db)
metadata.bind = db
session = create_session(bind=db)


resource_table = Table('resource', metadata,
Column('id',Integer, primary_key=True),
Column('name', String(30)),
Column('poly', String(31), nullable=True)
)

person_table = Table('person', metadata,
Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
Column('poly', String(31), nullable=False)
)

material_table = Table('material', metadata,
Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
)

employee_table = Table('employee', metadata,
Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

technical_table = Table('technical', metadata,
Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

class Resource(object):
def __init__(self, name):
self.name = name

def __repr__(self):
return Resource  id=%d ,name=%s  % (self.id,self.name)

class Person(Resource):
def __repr__(self):
return Person  id=%d ,name=%s  % (self.id,self.name)

class Material(Resource):
def __repr__(self):
return Material  id=%d ,name=%s  % (self.id,self.name)

class Employee(Person):
def __repr__(self):
return Employee  id=%d ,name=%s  % (self.id,self.name)

class Technical(Person):
def __repr__(self):
return Technical  id=%d ,name=%s  % (self.id,self.name)

mapper(Resource, resource_table,
polymorphic_on=resource_table.c.poly,
polymorphic_identity='resource'
)

mapper(Person, person_table, 
polymorphic_on=person_table.c.poly,
inherits=Resource, polymorphic_identity='person'
)

mapper(Material, material_table, 
inherits=Resource, polymorphic_identity='material'
)

mapper(Employee, employee_table,
inherits=Person, polymorphic_identity='employee',
)

mapper(Technical, technical_table,
inherits=Person, polymorphic_identity='technical',
)

metadata.create_all(bind=db)


r = Resource('resource name')
p = Person('person name')
m = Material('material name')
e = Employee('employee name')
t = Technical('technical name')

session.save(r)
session.save(p)
session.save(m)
session.save(e)
session.save(t)

session.flush()
print  LIST FROM RESOURCES #

for o in session.query(Resource).all():
print o

print  LIST FROM PERSONS #

for o in session.query(Person).all():
print o


code-8--code--

The results:

 LIST FROM RESOURCES #
Resource  id=1 ,name=resource name 
Person  id=2 ,name=person name 
Material  id=3 ,name=material name 
Employee  id=4 ,name=employee name 
Technical  id=5 ,name=technical name 
 LIST FROM PERSONS #
Person  id=2 ,name=person name 
Employee  id=4 ,name=employee name 
Technical  id=5 ,name=technical name 



I think it is working properly.

but I will try to remove the type columns

Thank's for help
-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread Alexandre da Silva


Em Qui, 2008-01-10 às 21:20 -0500, Rick Morrison escreveu:
 You're mixing single-table inheritance (using the discriminator
 column), with concrete inheritance (using multiple tables). 
 
 You have to pick one scheme or the other. Either use a single
 inheritance chain, or separate the two class hierarchies into two
 separate chains that don't inherit from each other. In the separate
 scheme, each chain can use it's own discriminator column, but you
 cannot combine two class hierarchies that each use a different
 discriminator column. 
 

In fact I am doing what is sugested here:
http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_mapper_inheritance_joined
It works fine with one level inheritance  class-subclass
but the third subclassing don't working, it was because objects was in
cache... savint to db and tryin to load again the identity is lost.

how do you suggest to I do this? or I cannot?

-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Get default value

2008-01-10 Thread sdobrev

 How many levels I can inherit classes/tables without get something
 wrong?

my tests go to 4, all works. And as all corner cases are already there,
i guess any level above will work too.
mixed inheritance (joined+concrete) also can be made to work, as long as 
polymoprhic_union() is fixed slightly - AND no real polymoprhism over 
concrete tables, that does not work on SA level / conceptualy.

may i suggest, that u get dbcook.sf.net, model your whole hierarchy there 
(it is SIMPLE), then run it in generator mode (see usage/example/*) and see 
what equivalent SA source/calls it generates.
maybe you are missing something (some mapper/relation parameters are tricky 
to guess). be ware, only joined_inheritance is of real use (single_table is 
not implemented).

 let me show a simplest sample hierarchy:
 
   resource
 / \
   person   material
/  \
  employeecustomer
 
 
 now, I am creating a type column on resource to map persons and
 materials, them I am creating another type column on person, to get
 mapped the various types of persons.
no u dont do it this way. u musthave only one discriminator column per 
hierarchy-island.
either use the root one, and put all types there, or separate the material 
from resource in its own subhierarchy. If there is explosion of  types, to 
avoid the huuuge union/outerjoin, u can make the resource a virtual base, 
that is, not a table at all - so u'll have two separate db-hierarchies, each 
one with its own root/discriminator. (dbcook: just declare 
DBCOOK_no_mapping=True there.)

 resource_table = Table(
   Column('id',Integer, primary_key=True),
   Column('poly', String(31), nullable=False)
 )
 
 person_table = Table(
   Column('id',Integer, primary_key=True, ForeignKey('resource.id'),
 primary_key=True)),
   Column('poly', String(31), nullable=False)
 )
u should not have poly here. its already in the root=resource.

 employee_table = Table(
   Column('id',Integer, primary_key=True, ForeignKey('person.id'),
 primary_key=True)),
 )
 
 class Resource(object):
   pass
 class Person(Resource):
   pass
 class Employee(Person):
   pass
 
 mapper(Resource, resource_table,
 polymorphic_on=resource_table.c.poly,
 polymorphic_identity='resource'
 )
 
 mapper(Person, person_table, 
 polymorphic_on=person_table.c.poly,
 inherits=Resource, polymorphic_identity='person'
 )
either put  the correct polymorphic_on=resource.c.poly, or remove it 
alltogether,
it comes from the inherited base-mapper.

 mapper(Employee employee_table,
 inherits=Person, polymorphic_identity='employee',
 )

svilen


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---