Thanks for the replies!
if I leave the class person like this:

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

This is SQL generated:

CREATE TABLE "person_person" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL,
)
;
CREATE TABLE "employee_employee" (
    "person_id" integer NOT NULL UNIQUE,
    "address_id" integer NOT NULL PRIMARY KEY,
)

And when I need to get the employee data, I call Person or Employee?
I can call the Employee who is using the person_id?

Thanks!!



On 16 abr, 20:06, Ian Clelland <[email protected]> wrote:
> On Sat, Apr 16, 2011 at 1:35 PM, Guevara <[email protected]> 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
> <[email protected]>

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

Reply via email to