0

I have a Django model as:

Class DataFile(models.Model):
    file = models.FileField(upload_to=get_file_upload_path)

But, file is a reserved keyword in MySQL.

I know that we can use any reserved keyword as column name by wrapping it in ``.

My senior advised me not to use reserved keywords and use something which is nearly meaningful other than just file.

What are the cons?

Sreekanth Reddy Balne
  • 3,264
  • 1
  • 18
  • 44

1 Answers1

2

But, file is a reserved keyword in MySQL.

That is not a problem when you use Django. You can use the db_column=… parameter [Django-doc] to use a different name at the database level.

For example:

class DataFile(models.Model):
    file = models.FileField(upload_to=get_file_upload_path, db_column='filepath')

So now you can access the field at the Django level with my_data_file.file, but at the database side, the column is named filepath. Django will make thus queries to the filepath column.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Do you know what problems I might face in the future if I use reserved keywords? Ignore django. – Sreekanth Reddy Balne Nov 19 '19 at 10:47
  • @spiritsree: please don't use reserved keywords for the database level. It makes it a lot easier to make mistakes later. For example if you write another program that works with the database: https://stackoverflow.com/a/23446378/67579 – Willem Van Onsem Nov 19 '19 at 10:49