dtjokro
dtjokro

Reputation: 122

Best practices Boolean vs Integer in SQL

i'd like to ask questions regarding this case scenario. Supposedly that i have 3 tables.

> class Event(models.Model):
>     class Meta:
>         verbose_name = _('Event')
>         verbose_name_plural = _('Events')
> 
>     def __str__(self):
>         return self.name
> 
>     name          = models.CharField(max_length = 255, null = True)
>     description   = models.TextField(null=True)
>    
>     organizer   = models.CharField(max_length=255, null=True)
>     date_from   = models.DateTimeField(null=True)
>     date_to     = models.DateTimeField(null=True)
> 
>     address         = models.ForeignKey('location.Address', null=True, blank=True, related_name="event_address")
>     active = models.BooleanField(default=False)

> class CityNomad(models.Model):
>     class Meta:
>         verbose_name = _('City Nomad')
>         verbose_name_plural = _('City Nomads')
> 
>     uid       = models.IntegerField()
>     event     = models.ForeignKey('event.Event', null=True, blank=True, related_name='citynomad_event')

> class YourSingapore(models.Model):
>     class Meta:
>         verbose_name = _('Your Singapore')
>         verbose_name_plural = _('Your Singapore')
> 
>     event   = models.ForeignKey('event.Event', null=True, blank=True, related_name='citynomad_event')
>     ys_priority = models.IntegerField()

Then i have views using django rest framework that will retrieve the either Event, Travelrave only events, and then CityNomad only events.

so i'm thinking of three ways:

1. insert two columns on Event model.

is_travelrave = models.BooleanField()

is_citynomad = models.BooleanField()

2. insert a single integer

event_identifier = models.IntegerField() (identify 1 as travelrave, 2 as citynomad)

3. create a table called event_identifier (with id, and event_identifier_name)

event_identifier = models.ForeignKey('event_identifier')

Will having too many boolean columns affect the performance? Does having a foreign key redundant?

Upvotes: 0

Views: 184

Answers (1)

DanEEStar
DanEEStar

Reputation: 6280

You don't have to do anything manually.

If you want to get all CityNomad events you can get them like so:

Event.objects.filter(citynomad_event__isnull=False)

But you have to pay attention to the related_name property. In the YourSingapore-model it is the same as the CityNomad related_name-property.

Upvotes: 2

Related Questions