Model = to_model(app_label, self.name)Ĭreate_table = 'CREATE TABLE %s' % schema_editor.quote_name( Schema_nnection, collect_sql=True, atomic=FalseĪpp_label, collector, from_state, to_state from django.db.migrations import CreateModelĬlass CreatePartitionedModel(CreateModel):ĭef _init_(self, name, fields, partition_sql, **kwargs):ĭef database_forwards(self, app_label, schema_editor, from_state, to_state): That's what powers the sqlmigrate feature by the way.
#Postgresql 10 vs 11 code#
I went through the Django create_model code and the sql is generated and directly send to the database, so it would not be easy to extract the statement from there.ĭoes anybody has a clue how this could be achieved in a way in which I can I still use the benefits of the Django ORM?Īn approach I suggest trying is to use a SQL capturing schema editor to collect the SQL necessary to perform the create_model. I couldn't find any method that returns the statement.
Then, I could append the PARTITION BY clause, and send the query directly. sql = Measurement.get_statement() where Measurement is the model. Unfortunately, the above (or similar) doesn't seem to be supported in PostgreSQL ALTER TABLE statement(at least not yet).Ī final idea would be to retrieve the CREATE TABLE statement that the Django model generates, before sending the query, e.g. "ALTER TABLE measurement PARTITION BY RANGE (logdate)", I have also though of customizing the migrations, by adding an ALTER TABLE. Instead, it uses inheritance and triggers so the code does not suggest any way in which I could append the clause (neither it does for other databases, e.g. I have also look into the architect library, but it does not use the new PARTITION BY clause. using the Meta: class Measurement(models.Model):Īs far as I am concern the above is not possible.
Is it possible to insert this clause into the model? I thought that maybe there is a way to somehow append custom SQL to the query that the ORM generates, e.g. In principle all what I would need to do is introduce the PARTITION BY clause at the end of the CREATE TABLE statement that the Django ORM creates. Next stepsįor information on supported PostgreSQL extensions, see the extensions document.PostgreSQL 10 introduces declarative table partitioning with the PARTITION BY clause, and I would like to use it to a Django model. Version 10 to 11 is a major version upgrade. For example, 10.0 to 10.1 is a minor release upgrade. As of version 10, only a change in the first number is considered a major version upgrade. For example, 9.5 to 9.6 was considered a major version upgrade.