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.
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.
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.
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!
I am currently developing a server using Flask/SqlAlchemy. It occurs that when an ORM model is not present as a table in the database, it is created by default by SqlAlchemy.
However when an ORM class is changed with for instance an extra column is added, these changes do not get saved in the database. So the extra column will be missing, every time I query. I have to adjust my DB manually every time there is a change in the models that I use.
Is there a better way to apply changes in the models during development? I hardly think manual MySql manipulation is the best solution.
you can proceed as the following:
new_column = Column('new_column', String, default='some_default_value')
new_column.create(my_table, populate_default=True)
you can find more details about sqlalchemy migration in: https://sqlalchemy-migrate.readthedocs.org/en/latest/changeset.html
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.