I am using Django to develop new software to replace a legacy one, and all my new models have auto-increment primary keys. I want to import records from the legacy database keeping their original primary keys (users know them all), but I don't know how to do it with auto-increment primary keys.
Is it possible to create my models with integer primary keys and change them to auto-increment ones without losing their data?
It is possible to have an IntegerField as primary key without auto-increment feature. You can do this by
id = models.PositiveIntegerField(primary=True)
And once you import all your data, you can convert it to AutoIncrement by replacing PositiveIntegerField to AutoField.
Solution-2
You can specify id of your row during data import while having default AutoField and it will load data properly. So no need worry here.
Related
I migrated my Django database from Sqlite into Postgres, the data is properly set and tested when reading.
The thing is that, when I try to add a new registry into the table, Django is using id number as 1 instead to be the last registry id plus one.
That of course returns an error, something like this:
IntegrityError at /admin/accounting/expense/add/
duplicate key value violates unique constraint "accounting_expense_pkey"
DETAIL: Key (id)=(2) already exists.
How can I make Django to use the proper id number when trying to save a registry?
I'm aware there is a way to set Django to begin the count on a specific number, but I wanted to know if there is a way to tell Django to look at the database (which is populated) and use that id count.
In addition to my primary key, I would like to set up one or more alternate keys in my model in Django. For example, perhaps the primary key in the customer table is a Django-generated ID. But, I may want another, alternate key to ensure that no customer-name, insert-date combination is duplicated. Is there a way to do this in Django or must I go out and do this in the database?
Thanks!
Here's what I want to do: I need to migrate some models to another database modifying some of its attributes. The approach I came with was set another database setting in my DATABASES settings named 'production'.
Then I do this:
for cat_to, cat_from in category_names.items():
c_f = InRadarCategory.objects.get(name=cat_from)
c_t = InRadarCategory.objects.get(name=cat_to)
for ad in InRadarAd.objects.filter(category=c_f):
ad.pk = None
ad.category = InRadarCategory.objects.using('production').get(name=c_t.name)
ad.save(using='production')
Note that, before saving it to the other database, I modify it's category attribute with an object that's in the other database. After that, I try to push it in by calling save on ad instance, but using the production database.
That gives me the error:
instance is on database "default", value is on database "production"
Django docs states here that:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
Question is: Am I at the right path but missing something silly? Is there a alternative approach to achieve this?
I'm creating an app in django that will eventually be filled with data from its predecessor. I'd like to have certain models start their auto-increment counter at 10000 to differentiate this data in advance and keep the pk accounting consistant. How can I do this in the model? AutoField doesn't seem to take parameters that would let me do this.
This isn't a question about Django. AutoField is simply a representation of the underlying database's autoincrement property, and Django doesn't control it. Depending on your database backend, you might be able to reset the autoincrement start value: look at your db's documentation for details.
I have a Django project that started as an import of a legacy database. Because of this, there is a model with a composite primary key. This worked as long as I used only the legacy data, but now I want to add new data and the form I created is telling me that I am trying to insert duplicate data, presumably because it is only looking at one of the fields as the primary key.
Now I want to change the model to use an autoincrement primary key, like one Django would automatically add. I tried removing the primary key attributes from the fields and putting them in unique_together in the Meta inner class. When I ran schemamigration with South, it wanted to add an id field as expected, but it asked for a default value.
How can I specify that South should assign unique keys in some way that is reasonable for an autoincrement field? (i.e. assign the sequence [1...n] to some arbitrary ordering of the records)
If this is impossible is there another way to accomplish the same thing, preferably using Django and South?
I solved the problem that required me to do this with a workaround:
I copied the data from the original table into a temporary table in SQL with INSERT INTO... SELECT.... I then deleted the original table and recreated it with the autoincrement field. Then I copied the data back into the new table, with the autoincrement values added automatically by the INSERT command. Finally I executed a fake run of the South migration to make South's tables consistent with the new schema.