Re: Database records being overriden with a multi-table inheritance schema

40 views
Skip to first unread message

Marcelo Mendes Pereira

unread,
Dec 26, 2012, 8:50:00 AM12/26/12
to django...@googlegroups.com
The problem seems to happen because of both PersonCustomer and LegalEntityCustomer inheriting from Customer, and their primary key being a foreign key to Customer primary key, when as LegalEntityCustomer is created its primary key is set to 1 the same of PersonCustomer and that way both references the first Customer created, the desired behavior should be create a customer to each PersonCustomer or LegalEntityCustomer, anybody know how to correct this problem? in the model schema definition?

Em quarta-feira, 26 de dezembro de 2012 10h13min20s UTC-2, Marcelo Mendes Pereira escreveu:
Hi, i am trying to model the database for a project that i am developing i have the following models

class CommonInfo(models.Model):
#fields
class Meta:
abstract = True
class Person(CommonInfo):
#fields
class LegalEntity(CommonInfo):
#fields
class User(models.Model):
user = models.OneToOneField('auth.user', unique=True)
class Meta:
abstract = True
class Customer(User):
        #implicit user referece to a django user 'auth.user'

class PersonCustomer(Person, Customer):
        #implicit user referece to a django user 'auth.user'

class LegalEntityCustomer(LegalEntity, Customer):
        #implicit user referece to a django user 'auth.user'

I created the ModelAdmin to each model and setted a custom form to create a django user every time a customer is created, but the problem i am getting is that when i create a PersonCustomer (the custom form creates a django user as expected) and after that a LegalEntityCustomer (again another django user is created as expected) the PersonCustomer has its user reference changed to the django user created to LegalEntityCustomer, actually if i change the order of creation the same thing happens but this time the LegalEntityCustomer that has it's user reference changed, anybody know what i am doing wrong? Thanks, in advance.

Gerald Klein

unread,
Dec 26, 2012, 10:49:39 AM12/26/12
to django...@googlegroups.com
It seems like you have the same idea recurring twice in this model. This IMHO doesn't require multiple inheritance. I would call commoninfo customer with it's fields and inherit django user then Person and LegalEntity inherit customer (which inherits django user) and add on their specific fields and skip the PersonCustomer and the LegalEntityCustomer. 

Customer would inherit the django userid, and so then would person and legalentity. They could still be identitied with an 'is a' customer' or 'is a' Person or legal entity. 

hope this helps

--jerry

On Wed, Dec 26, 2012 at 7:50 AM, Marcelo Mendes Pereira <mpmar...@gmail.com> wrote:
Customer



--

Gerald Klein DBA

Cont...@geraldklein.com

www.geraldklein.com

geraldklein.wordpress.com

j...@zognet.com

708-599-0352


Arch Awesome, Ranger & Vim the coding triple threat.

Linux registered user #548580 


Marcelo Mendes Pereira

unread,
Dec 26, 2012, 11:58:39 AM12/26/12
to django...@googlegroups.com, j...@zognet.com
Thanks for the reply. I omitted my entire schema i should had mentioned that i have the models Seller, Supervisor (which is a seller and supervises other sellers) and Manager and the seller can be a Person and LegalEntity too that is why i didn't made Person or LegalEntity inherit from Customer because not all Persons or LegalEntities are customers. 

Marcelo Mendes Pereira

unread,
Dec 26, 2012, 12:07:01 PM12/26/12
to django...@googlegroups.com
I think a i figured out a solution but i don't have a complete understanding about the problem to explain exactly what i was doing wrong, but here is my schema now

class CommonInfo(models.Model):
class Meta:
abstract = True

class User(models.Model):
user = models.OneToOneField('auth.user')

class Person(User, CommonInfo):
# specific fields
pass

class LegalEntity(User, CommonInfo):
# specific fields
pass

class Seller(User):
# specific fields
pass

class PersonSeller(Person, Seller):
# specific fields
pass

class LegalEntitySeller(LegalEntity, Seller):
# specific fields
pass

class Supervisor(Seller):
# specific fields
supervisees = models.ManyToManyField('Seller', related_name='supervisors')

class Customer(User):
# specific fields
pass

class PersonCustomer(Person, Customer):
# specific fields
pass

class LegalEntityCustomer(LegalEntity, Customer):
# specific fields
pass

class Manager(User):
# specific fields
pass

Unfortunately now i have a table user that control the generation of the dynamic ids to the models ensuring that i will not have a LegalEntityCustomer or PersonCustomer refering the same Customer, and as i needed a field user to refer to a django user i took the opportunity and used the same model User to define this field.

Gerald Klein

unread,
Dec 26, 2012, 12:42:15 PM12/26/12
to django...@googlegroups.com
I can't see from this a clear hierarchy, please describe relationships of supervisors and managers etc. make hierarchy block diagram to show how parts relate. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/U_5SkUFAxRIJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Gerald Klein

unread,
Dec 26, 2012, 12:44:50 PM12/26/12
to django...@googlegroups.com
I think you are a little to micro what is the difference between a Person and the other entities are they all not people? 

On Wed, Dec 26, 2012 at 11:07 AM, Marcelo Mendes Pereira <mpmar...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/U_5SkUFAxRIJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages