Does Django support alternate keys in DB models? - django

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!

Related

Django: Import legacy database data keeping primary keys having an autokey

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.

Does Django support setting the beginning value for an id column?

I have seen several questions and answers on SO, most were three years old or older and I looked at the Django documentation (hoping I didn't miss it). I have to have a 9+ digit number for an id. Most responses were to do this at the database. I am guessing that means to create the model in Django and then go back to the database and change the id column Django created with a new starting/next value attribute on the column.
If not how can I create a database table from Django, Code First, that allows me to create a table with an id column that starts at 100000000? And, it be done with the stock model object methods in Django. I don't really want to do a special hack. If that is the case, I can go the database and fix the column. I was trying to adhere to the Code First ideas of Django (though I prefer database first, and am afraid using inspectdb will make a mess.)
Edit: I didn't want to use UUID. I believe BigAutoField is best.
You should be able to do this in two steps:
1 - Specify your primary key explicitly using primary_key=TRUE in your model definition. See the Django docs for more info. You can then specify BigAutoField or whatever other type you want for the primary key.
2A - If you're populating the database up front, just set pk: 100000000 in your fixture.
OR
2B - If you're not populating the database up front, use Django Model Migration Operations RunSQL as detailed here. For your SQL use ALTER TABLE tableName AUTO_INCREMENT=100000000.

Initial value for django model autofield

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.

Adding Autoincrement field to existing model with Django South?

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.

one primary key column foreign key to 2 other table columns.How to resolve data entry issue

I have a requirement according to which I have to create a central Login system.We have 2 things Corporate and Brand each represented by tables "Corporate" and "Brand".
When a corporate gets registered,corporateID is given,When a user under that corporate gets registered there is a table corporateuser in which corporateID is a foreign key and CorporateUserID is a primary key.Similarly in the case of a brand.
So we have CorporateUserId and BrandUserID.
Now i have a table called RegisteredUsers in which i want to have corporate as well as brand users.UserID is a primary key in this table which is a foreign key to both corporateuser as well as Branduser.
now when i enter a corporateuser,I do an entry to corporateuser as well as RegisteredUsers.When i enter CorporateUserID in userID for RegisteredUsers.It gives foreign key violation error.
I fully understand this error.How can i achieve this.This requirement is very rigid.Please tell a workaround
What you're trying to do is not totally clear, but it seems that you want the primary key of all three user tables to be the same. This is not a strict foreign key relationship, but it seems reasonable in your application.
You need to assign the userID in RegisteredUsers first, and use that key when you create your Corporate User or Brand User. Then the user id's will be unique across the whole system.
If that's not what you want, edit your entry with the table layouts to make the problem clearer.
If you are trying to insert records into tables with relational conatraints, you will need do all inserts under one SQL Transaction.