Code

Opened 6 years ago

Closed 6 years ago

#6556 closed (fixed)

Custom subclass of models.CharField does not appear in the CREATE SQL

Reported by: jarrow Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

If I subclass models.CharField (without overwriting anything) it seems to work perfectly but the field is not created on syncdb anymore. Explicitly defining get_internal_type() fixes this.

That's the field (working)

class Event(models.Model):
    urania_event_id = models.CharField("foo", max_length=4, unique_for_year="time")

Subclassed without changing anything.

class UraniaEventID(models.CharField):
    pass
    
class Event(models.Model):
    urania_event_id = UraniaEventID("foo", max_length=4, unique_for_year="time")

Effect: 'urania_event_id' does not appear in CREATE SQL anymore

Workaround:

class UraniaEventID(models.CharField):
    def get_internal_type(self):
        return "CharField"

class Event(models.Model):
    urania_event_id = UraniaEventID("foo", max_length=4, unique_for_year="time")

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from newforms-admin to SVN

Whether or not this is a problem, I highly doubt this is specific to newforms-admin as it has NOTHING to do with the database wrapper. Please verify this is the case on SVN, regardless if this is an actual problem or not. If I am wrong, please do correct me, but with some solid evidence :)

comment:2 Changed 6 years ago by jarrow

Ok. Confirmed on SVN r7092. Still the same. I just thought I'll tell the truth that it wasn't tested on trunk :)

comment:3 Changed 6 years ago by mackenzie

I can also confirm this.

Also thanks for the workaround!!!

comment:4 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(In [7133]) It makes sense that when you subclass an existing model field, you're often
going to be using the same database column type. Made that properly
inheritable (previously it was using the class name), at the cost of a little
more verboseness.

This is very slightly backwards incompatible (for subclasses of existing fields
that were relying on the old default).

Fixed #6556.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.