On django model.

125 views
Skip to first unread message

Amor Zamora

unread,
Dec 24, 2021, 7:12:36 AM12/24/21
to django...@googlegroups.com
Hi guys.
I have an application in Django that has a database in postgresql.
But a new table was added to that database.
I have tried using the inspectdb (python3 manage.py inspectdb tracking_visit) command, to add that table to the DJango model, but it gives me an exception, it tells me that the ID field of the new table is not an integer and if it is, I put it as an integer and primary key and so on itself is reflected in the database.
# Unable to inspect table 'tracking_visit'
# The error was: sequence index must be integer, not 'slice'

1. My question is if you know any other method or application that allows me to generate the model from the database.
2. Is there any other way to include changes made from the database into the django model?

Best regards
--
Amor Zamora
Behind the distance, 
your mouth and mine hide, 
complices of a kiss, caresses, fantasies, 
distance as close as the sky and the sea, 
my piece is in your bed, in mine, is your love.
Behind the distance hide love, memories, 
encounters, experiences, circumstances, pain and good times, 
it is after her, the distant ones, 
that we leave feelings, 
but if we really miss, 
distance,
it is only your time.

Sebastian Jung

unread,
Dec 24, 2021, 8:03:51 AM12/24/21
to django...@googlegroups.com
Hello,

Insectdb is not to generate/update database. Please make it with python manage.py makemigrations and after that python manage.py migrate

Please read a tutorial https://tutorial.djangogirls.org/de/

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKMTbHUWN5QaAyrgrABL5GeKouU0qf2xoxkUqpg-x4WJZmocvg%40mail.gmail.com.

Muhammad Juwaini Abdul Rahman

unread,
Dec 24, 2021, 8:07:04 AM12/24/21
to django...@googlegroups.com
I think you're the one who don't fully understand Amor's email.

Amor Zamora

unread,
Dec 24, 2021, 8:34:59 AM12/24/21
to django...@googlegroups.com
I think he did not understand the situation, because with migrations I don't resolve to convert the database into the model. The process that I need is the other way around.
I needs  for a table created in the database to be included in the model.

Sebastian Jung

unread,
Dec 24, 2021, 8:41:12 AM12/24/21
to django...@googlegroups.com
Sorry i don't understand nothing what do you say... Please write us a example what yoz want to do. Normal case is that you define your model in model.py and after this you make makemigrations ans after that migrate...

Muhammad Juwaini Abdul Rahman

unread,
Dec 24, 2021, 8:46:30 AM12/24/21
to django...@googlegroups.com
Have you checked the content of that table using dbshell?

I have suspicion that the ID in your table is not a mere bigint. 

According to your error message, it is 'slice' (I don't know what it is), so you need to investigate what kind of data stored before you can move forward from this issue.

Rafael Matos Borges

unread,
Dec 24, 2021, 11:23:19 AM12/24/21
to django...@googlegroups.com

Bernard Mallala

unread,
Dec 24, 2021, 12:58:30 PM12/24/21
to Django users
You can achieve this in two ways:
1.  python manage.py inspectdb --help
       Introspects the database tables in the given database and outputs a Django model module.
       Your id field should be a sequenced integer and  primary key so must include these two lines if viewing the CREATE SQL statement
       id integer NOT NULL DEFAULT nextval('appname_modelname_id_seq'::regclass) 
       CONSTRAINT ppname_modelname_pkey PRIMARY KEY (id),
2. Review the schema for the table that already has data and manually create a django model, make migrations and migrate.
I prefer this method as you get to create the mode the way you want it and they way it is best suited for your application.
Generate a create table statement using PGAdmin or any other database management tool
Use the create statement as a reference point to create the desired django model
Make changes to the table using pgadmin if needed so as to compliment the model the way you want it and as you create it
Execute makemigrates and migrate to create the table for this model
Now export the data from your table and use fixtures to import into the model

Amor Zamora

unread,
Dec 24, 2021, 7:45:01 PM12/24/21
to django...@googlegroups.com
I have done this but inspectdb generates an error and not the model.
When I do the migration it doesn't give me an error but it doesn't create the new log in the database.

El vie, 24 dic 2021 a las 14:03, Sebastian Jung (<sebasti...@gmail.com>) escribió:

Bernard Mallala

unread,
Dec 24, 2021, 8:19:52 PM12/24/21
to Django users
Inspectdb is generating an error because your table 's schema is causing a conflict. To solve this, you will need to post the exact error here and the schema of your table here for analysis. This is a longer process for analysis in case it is not immediately obvious.

My recommendation is for you to use the second approach i.e 
Create a new model in Django > makemigrations > migrate - this will result in the same table 
Then import the data into this table.

Gabriel Araya Garcia

unread,
Dec 26, 2021, 6:31:31 PM12/26/21
to django...@googlegroups.com
I make the changes in database table manually. The most important is the keep the amount of fields in the model and the type (varchar,integer, text, ..etc.)
That is part of Django flexibility.
 
Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




--
Reply all
Reply to author
Forward
0 new messages