How must be my mysql table with a PointField of models.py

Hello,

I have to rebuild my app from scratch and I lost my local databse. That’s not very important excepted for a type of a column.

In my models files, I added the following

location = PointField(blank=True, null=True, srid=4326)

In my database, I created a new column named ‘location’ but I am not very sure if the type must be POINT or GEOMETRY. In my point of view, it should be POINT

If in my batabase, I choose a wrong type, does makemigrations and migrate will correct/update it?

Thank for your clarification

Hi @pierrot10, the migration system wants to be the only thing modifying the database. Each of the migration files indicates how to change the database. After the initial migration files, they start to depend on previous migration files. If you don’t run one of them, you may not end up with a database in the same state.

Where I’m going with this is, if the database schema is changed outside of the migrations, the migrations aren’t going to know about it and won’t change because of it. You should try to use only the migrations to modify the schema of your database. This will make it easier to stand up an application in other environments (other dev machines, staging, production, etc).

I’d like to suggest a slight rephrasing of this.

The migration system wants to be the only thing modifying the tables, indexes, and sequences that Django is defined as managing.

Django doesn’t much care about models marked as managed=False or database entities not mapped into the Django project.

1 Like

Hello Thank for your reply, but I am sorry, I am afraid I did not understand well your observation.
My first worry is the type of my column ‘location’

As I added in my models
location = PointField(blank=True, null=True, srid=4326)
I supposed my column should a tyope of POINT and not GEOMETRY, isn’t?

After the initial migration files, they start to depend on previous migration files. If you don’t run one of them, you may not end up with a database in the same state.

Now regarding your comments, as I add the above line, the database schema should be changed after I launched makemigrations? and migrate should push the change to the DB?

Or should I “manually” change the schema file. In my lately App, I created a column column and I updated my models.py and it was fine. But now, I am curious to understand that mechanic.

I am double confuse, because I just ran ./manage.py inspectdb and it returned me this line
location = models.GeometryField(blank=True, null=True)

To clarify the objectif of ‘location’, I have the latitude saved into the column ‘station_lat’ and the longitude saved into the column ‘station_lng’. As far as I remember, my App or Django could not read the both column and I needed to create a column ‘location’ in order to have the latitude and the longitude in the same column and separeted by a coma.

The save() helped my to copy the lat and lng to the location

   class Stations(models.Model):
    id_station = models.AutoField(primary_key=True)
    fields_id_field = models.ForeignKey(Fields, models.DO_NOTHING, db_column='fields_id_field')
    stations_types_id_stations_type = models.IntegerField()
    station_name = models.CharField(max_length=20)
    station_longname = models.CharField(max_length=45, blank=True, null=True)
    station_active = models.BooleanField()
    station_archive = models.BooleanField()
    location = PointField(blank=True, null=True, srid=4326)
    station_lat = models.FloatField(blank=True, null=True)
    station_lng = models.FloatField(blank=True, null=True)
    station_alt = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    installed = models.DateTimeField()
    station_description = models.TextField(blank=True, null=True)
    ttn_app_id = models.CharField(max_length=20)
    ttn_dev_id = models.CharField(max_length=20)
    ttn_hardware_serial = models.CharField(max_length=20)
    station_created = models.DateTimeField()
    station_order = models.IntegerField()
    map = models.BooleanField()

    class Meta:
        managed = False
        db_table = 'stations'

    def save(self, *args, **kwargs):
        self.location = Point(float(self.station_lng), float(self.station_lat))
        super(Stations, self).save(*args, **kwargs)

As the aboce code worked before, I would like to keep it, but I have some doubt about the column type of my DB (POINT, GEOMETRY, other)

makemigrations will create the migration files say how to change your database.
migrate will execute the migration files to actually change your database.
Does that make sense?

Regarding the type of location, you need to decide what type it should be. Once you know that, we can then address the database. If you can start with a fresh database, I would suggest doing that. If you have to migrate a production database, then maybe manipulating the database manually may best. However, this assumes you don’t use managed = False on the model’s Meta class.