[sqlalchemy] Concrete Inheritance problem

2007-11-19 Thread Partha

I have 2 tables Person (id, name)  Employee (id, salary) and every
Employee 'isa' Person, so employee.id == person.id. I am trying to use
the Concrete Inheritance (i.e. ' pjoin) example provided in the
documentation. My mapping looks as follows.

person_table = Table(persons, __meta__,
 Column(id, Integer, primary_key=True),
 Column(name, String(80))
 )

employee_table = Table(empys, __meta__,
 Column(id, Integer, ForeignKey(persons.id),
primary_key=True),
 Column(salary, Integer),
 )

class Person (object): pass
class Employee(Person): pass

pjoin = polymorphic_union({
'person':person_table,
'employee':employee_table
}, 'type', 'pjoin')



person_mapper = mapper(Person, person_table, select_table=pjoin, \
polymorphic_on=pjoin.c.type,
polymorphic_identity='person')
emp_mapper = mapper(Employee, employee_table, inherits=person_mapper,
\
concrete=True, polymorphic_identity='employee')



I want to now add a New employee to the system with the following
snippet.

e = Employee()
e.name = 'TestEmpl'
e.salary = 100
session.save(e)
session.commit()

Problem is when it tries to save employee, sqlalchemy raises the
following error

sqlalchemy.exceptions.ProgrammingError: (ProgrammingError) relation
empys_id_seq does not exist
'select nextval(\'empys_id_seq\')' None

Since all id's in employees table map directly to Person table (which
has the correct sequence), I would think alchemy would first store the
Person part and then store the employee part. Any clues on how to
correct this ??.
I  also tried adding a Sequence to employee forcing it to use the same
one as person table...

employee_table = Table(empys, __meta__,
 Column(id, Integer, ForeignKey(persons.id),
Sequence(person_id_seq)  primary_key=True),
 Column(salary, Integer),
 )


But now it raises a constraint violation because its trying to insert
a row in employees table without inserting anything in person first..

Any ideas how I can fix this?.. I would greatly appreciate any help in
this regard.. Thanks

Partha


Here is the full program.. Thanks..

from sqlalchemy import create_engine, MetaData, Table, Column, types,
Sequence
from sqlalchemy import Table, Column, Integer, String, DECIMAL,
Numeric, ForeignKey, DateTime, Boolean, CHAR
from sqlalchemy.orm import mapper,sessionmaker, relation,
polymorphic_union
__meta__ = MetaData()

person_table = Table(persons, __meta__,
 Column(id, Integer, primary_key=True),
 Column(name, String(80))
 )

employee_table = Table(empys, __meta__,
 Column(id, Integer, ForeignKey(persons.id),
primary_key=True),
 Column(salary, Integer),
 )
class Person (object): pass

class Employee(Person): pass

pjoin = polymorphic_union({
'person':person_table,
'employee':employee_table
}, 'type', 'pjoin')



person_mapper = mapper(Person, person_table, select_table=pjoin, \
polymorphic_on=pjoin.c.type,
polymorphic_identity='person')
emp_mapper = mapper(Employee, employee_table, inherits=person_mapper,
\
concrete=True, polymorphic_identity='employee')


engine = create_engine('postgres:///test?user=postgrespassword=foo',
echo=True)
session = sessionmaker(autoflush=True, bind = engine,
transactional=True)()

def refresh(engine = engine):
__meta__.drop_all(engine)
__meta__.create_all(engine)

def main():
refresh()
e = Employee()
e.name = 'TestEmpl'
e.salary = 100
session.save(e)
session.commit()

if __name__ == '__main__':
main()

--~--~-~--~~~---~--~~
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: Concrete Inheritance problem

2007-11-19 Thread Partha

Thank you! . That worked great..

Partha

On Nov 19, 2:41 pm, [EMAIL PROTECTED] wrote:
 if it's about concrete inheritance, then employee contains ALL info it
 needs, that is, a full copy of person + whatever else is there,
 and is completely independent from person table.
 so for that case,
   a) foregn key is not needed
   b) inserting in employee_tbl will never insert stuff in person_tbl - they
 are independent.

 now, what u're expecting (chaining of id's and rows etc) will come from
 joined_table inheritance. See about that in docs; and just remove
 concrete=True from your mapper.

 Partha wrote:
  I have 2 tables Person (id, name)  Employee (id, salary) and every
  Employee 'isa' Person, so employee.id == person.id. I am trying to use
  the Concrete Inheritance (i.e. ' pjoin) example provided in the
  documentation. My mapping looks as follows.

  person_table = Table(persons, __meta__,
   Column(id, Integer, primary_key=True),
   Column(name, String(80))
   )

  employee_table = Table(empys, __meta__,
   Column(id, Integer, ForeignKey(persons.id),
  primary_key=True),
   Column(salary, Integer),
   )

  class Person (object): pass
  class Employee(Person): pass

  pjoin = polymorphic_union({
  'person':person_table,
  'employee':employee_table
  }, 'type', 'pjoin')

  person_mapper = mapper(Person, person_table, select_table=pjoin, \
  polymorphic_on=pjoin.c.type,
  polymorphic_identity='person')
  emp_mapper = mapper(Employee, employee_table, inherits=person_mapper,
  \
  concrete=True, polymorphic_identity='employee')

  I want to now add a New employee to the system with the following
  snippet.

  e = Employee()
  e.name = 'TestEmpl'
  e.salary = 100
  session.save(e)
  session.commit()

  Problem is when it tries to save employee, sqlalchemy raises the
  following error
  
  sqlalchemy.exceptions.ProgrammingError: (ProgrammingError) relation
  empys_id_seq does not exist
  'select nextval(\'empys_id_seq\')' None
  
  Since all id's in employees table map directly to Person table (which
  has the correct sequence), I would think alchemy would first store the
  Person part and then store the employee part. Any clues on how to
  correct this ??.
  I  also tried adding a Sequence to employee forcing it to use the same
  one as person table...

  employee_table = Table(empys, __meta__,
   Column(id, Integer, ForeignKey(persons.id),
  Sequence(person_id_seq)  primary_key=True),
   Column(salary, Integer),
   )

  But now it raises a constraint violation because its trying to insert
  a row in employees table without inserting anything in person first..

  Any ideas how I can fix this?.. I would greatly appreciate any help in
  this regard.. Thanks

  Partha

  Here is the full program.. Thanks..

  from sqlalchemy import create_engine, MetaData, Table, Column, types,
  Sequence
  from sqlalchemy import Table, Column, Integer, String, DECIMAL,
  Numeric, ForeignKey, DateTime, Boolean, CHAR
  from sqlalchemy.orm import mapper,sessionmaker, relation,
  polymorphic_union
  __meta__ = MetaData()

  person_table = Table(persons, __meta__,
   Column(id, Integer, primary_key=True),
   Column(name, String(80))
   )

  employee_table = Table(empys, __meta__,
   Column(id, Integer, ForeignKey(persons.id),
  primary_key=True),
   Column(salary, Integer),
   )
  class Person (object): pass

  class Employee(Person): pass

  pjoin = polymorphic_union({
  'person':person_table,
  'employee':employee_table
  }, 'type', 'pjoin')

  person_mapper = mapper(Person, person_table, select_table=pjoin, \
  polymorphic_on=pjoin.c.type,
  polymorphic_identity='person')
  emp_mapper = mapper(Employee, employee_table, inherits=person_mapper,
  \
  concrete=True, polymorphic_identity='employee')

  engine = create_engine('postgres:///test?user=postgrespassword=foo',
  echo=True)
  session = sessionmaker(autoflush=True, bind = engine,
  transactional=True)()

  def refresh(engine = engine):
  __meta__.drop_all(engine)
  __meta__.create_all(engine)

  def main():
  refresh()
  e = Employee()
  e.name = 'TestEmpl'
  e.salary = 100
  session.save(e)
  session.commit()

  if __name__ == '__main__':
 main()

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