Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set AUTOINCREMENT value in django table

I have the following table in mysql:

CREATE TABLE `portal_asset` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `asset_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;

How would I create thie same table in django? So far I have the following, but not sure how to set the AUTO_INCREMENT value --

class PortalAsset(models.Model):
    id = models.IntegerField(primary_key=True)
    asset_id = models.IntegerField(unique=True)
    class Meta:
        db_table = u'portal_asset'

How can I set the AUTO_INCREMENT value to start off at 1000000 ? The equivalent of:

alter table portal_asset AUTO_INCREMENT=1000000;
like image 448
David542 Avatar asked Nov 02 '25 08:11

David542


1 Answers

You can use a RunSQL operation in your migrations to execute the necessary SQL:

migrations.RunSQL("ALTER TABLE portal_asset AUTO_INCREMENT=1000000;")

If you haven't run any migrations, you can add this to your first migration to ensure no rows are inserted before the new value is set. Otherwise you'll have to add this operation in a new migration. You can create a new, empty migration using python manage.py makemigrations --empty <yourappname>.

like image 175
knbk Avatar answered Nov 03 '25 22:11

knbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!