On Sat, Apr 16, 2011 at 1:35 PM, Guevara <eguevara2...@gmail.com> wrote:

> Hello!
> I have two class, Person and employee, i need make a composition for
> this (Prefer composition over inheritance):
>
> class Person(models.Model):
>    name = models.CharField(max_length=50)
>    date_inclusion = models.DateField()
>    # others fields
>
> class Employee(models.Model):
>    person = models.OneToOneField(Person, primary_key=True)
>    address = models.OneToOneField(Address, primary_key=True)
>
>
> The SQL generate is:
>
>
> BEGIN;
> CREATE TABLE "person_person" (
>    "id" serial NOT NULL PRIMARY KEY,
>    "name" varchar(50) NOT NULL,
>    "date_inclusion" date NOT NULL,
> )
> ;
> CREATE TABLE "employee_employee" (
>    "person_id" integer NOT NULL PRIMARY KEY,
>    "address_id" integer NOT NULL PRIMARY KEY,
> )
> ;
>
>
> This is correct? Should generate the id of the employee?
>

I don't think it's correct -- a database table shouldn't have two distinct
primary keys. It's the "primary_key=True" part of your Employee model fields
that is doing this, and is also stopping an "id" field from being
automatically generated.

If you write Employee like this:

class Employee(models.Model):
   person = models.OneToOneField(Person)
   address = models.OneToOneField(Address)

Then it will generate SQL like this:

CREATE TABLE "employee_employee" (
   "id" serial NOT NULL PRIMARY KEY,
   "person_id" integer NOT NULL,
   "address_id" integer NOT NULL
)
;

which is probably closer to what you're expecting.

-- 
Regards,
Ian Clelland
<clell...@gmail.com>

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

Reply via email to