0

i want to start using a app with a specific id and when i migrate i have a error that is saying that i dont have permission or does not exist. Well i am already db_owner so maybe the error is when i do ALTER SEQUENCEon my migrations.

My Project

myproject/
     |-- myproject
     |-- dpo/
         |-- projeto/
            |-- models.py

Projeto model.py

class Projeto(models.Model):
  ..........

So i do python manage.py makemigrations dpo --empty and then i went to file and in opererations i add : migrations.RunSQL('ALTER SEQUENCE dpo_projeto_Projeto_id RESTART WITH 7000;') and i have this error Cannot alter the sequence 'dpo_projeto_Projeto_id', because it does not exist or you do not have permission.

Mehran Nouri
  • 136
  • 15
Pedro Mariz
  • 151
  • 2
  • 20

1 Answers1

1

You can follow this answer

If this won't work then follow these step

>>> u = Projeto.objects.create()
>>> Projeto.objects.filter(pk=u.pk).update(id=10000)

In create method you may require to pass model mandatory fields with value like this

Projeto.objects.create(name='Test Project')
shafik
  • 6,098
  • 5
  • 32
  • 50
  • i only need to put the `name='Test Project'` or i need to do that to all my mandatory fields ? – Pedro Mariz May 20 '19 at 14:33
  • as I have many required fields I created directly in the database and I did `u = Projeto.objects.filter(id=1)` but when i do the `Projeto.objects.filter(pk=u.pk).update(id=10000)` it is giving error `'QuerySet' object has no attribute 'pk'` so i did `Projeto.objects.filter(pk=u[0].pk).update(id=10000)` and i have another error `]Cannot update identity column 'id'.` – Pedro Mariz May 20 '19 at 15:16
  • i already emty the database, only have 1 id , and the value is `1` – Pedro Mariz May 20 '19 at 15:36
  • Change the value from where you want to start your primary key value – shafik May 20 '19 at 15:37
  • and how do i change? – Pedro Mariz May 20 '19 at 15:41
  • Projeto.objects.filter(pk=1).update(id=10000) then see what is the id of the objects in database – shafik May 20 '19 at 15:43
  • Ah SQL Server not permitted you.. All are the step already taken are write. But now I suggest you manually enter into database then change the value by SQL Server – shafik May 20 '19 at 15:50
  • i think i will drop and create datebase on sql server and on query i will do something like `[id] [int] IDENTITY(7000,1) NOT NULL,`. the only problem is that when i drop table, i need to delete my `FOREIGN KEY constraint` and make it again – Pedro Mariz May 20 '19 at 15:53
  • Apology! I does not work with SQL Server, that why I can't help you about this. I test it on sqlite3, PostgreSQL and it's working fine. – shafik May 20 '19 at 15:54
  • I did as I said I would and it's working thanks for everything – Pedro Mariz May 20 '19 at 15:57