0

I am new to Django.I have two models, Model A and a new Model B

class A:
    firstname=models.CharField(max_length=20,blank=True,null=True)
    email = models.EmailField(max_length=30,blank=True,null=True)

I have to migrate all the data from A to B in such that a way that primary key of a entry in Model A will be same in Model B. i.e

b.id = a.id where a and b are instance of A and B respectively.

but after this when i save a new instance the id generated is 5L, 6L etc. instead of incrementing the prmiary key of the last object created. Is there any way to fix this ?? I am using django 1.3 with postgresql 9.2.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Harshal
  • 117
  • 2
  • 10
  • why don't you use sql dump data from table A to B. – Shreeyansh Jain Jun 13 '13 at 10:41
  • 1
    You need to do this in the database, by setting the relevant sequence. I don't know enough about Postgres to give a proper answer, but it'll be in the documentation (http://www.postgresql.org/docs/9.2/static/sql-altersequence.html looks promising). – Daniel Roseman Jun 13 '13 at 10:45
  • @Daniel Roseman: Thank you. It did it at my db shell. – Harshal Jun 13 '13 at 11:05
  • @AnshJ : i am no sql expert so dont know how to implement through your above mentioned technique. – Harshal Jun 13 '13 at 11:06

1 Answers1

0

You can copy data from table A into B using insert command for example :

INSERT INTO A (id, firstname, email) SELECT b.id, b.firstname, b.email FROM B b;

Hope this will help you.

Shreeyansh Jain
  • 1,407
  • 15
  • 25
  • This works fine.... 1 more thing i would like to ask... Model A has been a foreign key to some models(like G,H, J). can i replace A with B. – Harshal Jun 14 '13 at 06:07
  • Their is one similar question on stackover flow for this (to update forignkey constraint) have a look into it... http://stackoverflow.com/questions/11073706/how-to-alter-a-mysql-tables-foreign-key-using-the-command-line – Shreeyansh Jain Jun 14 '13 at 06:15