Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#343 closed defect (fixed)

One-to-one relations - different primary_key

Reported by: Bless Owned by: Adrian Holovaty
Component: Metasystem Version: master
Severity: normal Keywords: One-to-one
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When is used a different primary_key (no integer) in One-to-one relations, id is always a integer

class Nic(meta.Model):
    fields = (
        meta.CharField('name', maxlength=4),
    )
class Network(meta.Model):
    fields = (
        meta.OneToOneField(nics.Nic),
        meta.CharField('name', maxlength=3, choices=NETWORK_TYPES, unique=True),
    )

BEGIN;
CREATE TABLE nics_nics (
    id integer NOT NULL PRIMARY KEY,
    name varchar(4) NOT NULL
);
CREATE TABLE nics_networks (
    id integer NOT NULL PRIMARY KEY REFERENCES nics_nics (id),
    name varchar(3) NOT NULL UNIQUE
);
COMMIT;

id integer NOT NULL PRIMARY KEY REFERENCES nics_nics (id), -> OK


class Nic(meta.Model):
    fields = (
        meta.CharField('name', maxlength=4, primary_key=True),
    )
class Network(meta.Model):
    fields = (
        meta.OneToOneField(nics.Nic),
        meta.CharField('name', maxlength=3, choices=NETWORK_TYPES, unique=True),
    )

BEGIN;
CREATE TABLE nics_nics (
    name varchar(4) NOT NULL PRIMARY KEY
);
CREATE TABLE nics_networks (
    id integer NOT NULL PRIMARY KEY REFERENCES nics_nics (name),
    name varchar(3) NOT NULL UNIQUE
);
COMMIT;

id integer NOT NULL PRIMARY KEY REFERENCES nics_nics (name), -> Bug
It should be varchar in this case.

Change History (5)

comment:1 Changed 11 years ago by Adrian Holovaty

Component: Core frameworkMetasystem

comment:2 Changed 10 years ago by James Bennett

Severity: majornormal
Version: 1.0SVN

The problem here is that OneToOneField inherits from IntegerField. Fixing it will require changing that, and the question is whether to inherit directly from Field as foreign keys do, or to inherit from ForeignKeyField (since that's what it ends up being in the DB).

comment:3 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

Fixed in r3846.

comment:4 Changed 10 years ago by Jacob

(In [4558]) Fixed #343: filters that take strings now handle non-strings correctly. Thanks to Boffbowsh for the original patch, and to SmileyChris for the updated patch and tests.

comment:5 Changed 10 years ago by Jacob

(oops - shoulda said I fixed #393...)

Note: See TracTickets for help on using tickets.
Back to Top