python - Using Django with an existing database id field trouble -


i'm using django existing database. used inspectdb create models of existing database. works except 1 important detail. seems though django assumes there id field. example, when delete field called date run migration, date deleted table. however, when try delete id field, says -alter field id on table_name if there no id field in actual database table.

so, if want make , use id field in existing database have manually specify in model , database or there better way?

by default django creates id field models if don't specify it, so, in case, if delete autogenerated:

id = models.integerfield(primary_key=true) 

django models assume 1 exists, message see. because need have 1 primary key field.

so, yes, have specify primary key adding primary_key=true attribute appropriate field on model generated inspectdb, or django assume primary key column id.

keep in mind that, if decide add id column table, shouldn't set explicitly in model:

each generated model has attribute every field, including id primary key fields. however, recall django automatically adds id primary key field if model doesn’t have primary key. thus, you’ll want remove lines this:

id = models.integerfield(primary_key=true)

not these lines redundant, can cause problems if application adding new records these tables.

if decide not to, make sure column end choosing pk doesn't have null nor repeated values.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -