0

I have a database with around 25 tables and I would like to automatically generate the model file for my python project. For developing the project I am using PyCharm with a MariaDB as RDMS.

The basic settings for the database have been done. In the next step I switch to the manage.py console to run the first command:

inspectdb

This is working fine. Each table will be listed in the output of the console window. Now I want to write the output in a file. Therefore I use the following command:

inspectdb > models.py

This ends up in an Error:

Unable to inspect table '>'
# The error was: (1146, "Table 'mydatabase.>' doesn't exist")
# Unable to inspect table 'models.py'
# The error was: (1146, "Table 'mydatabase.models.py' doesn't exist")

Process finished with exit code 0

The command is according to the documentation 'python manage.py inspectdb > model.py'. But in this case the '>' symbol will be intepreted as table, or a tablename has to be given.

In a second case, I try to export only one table:

inspectdb users > models.py

this ends up with the following output:

class Users(models.Model):
    name = models.CharField(max_length=400)
    username = models.CharField(max_length=150)
    email = models.CharField(max_length=100)
    password = models.CharField(max_length=100)
    .....

    class Meta:
        managed = False
        db_table = 'users'
# Unable to inspect table '>'
# The error was: (1146, "Table 'mydatabase.>' doesn't exist")
# Unable to inspect table 'models.py'
# The error was: (1146, "Table 'mydatabase.models.py' doesn't exist")

Process finished with exit code 0

Same problem here! In this case the model users is generated, but it is not written to the models.py file. What do I have to do, to save the generated models into my models.py file?

Perino
  • 608
  • 9
  • 30

2 Answers2

1

I'm assuming you're using a bash shell. If not, that could be your issue. You need to use the relative path to models.py for this to work. If your directory structure looks like this:

mysite/
    manage.py
    mysite/
    myapp/
        __init__.py
        admin.py
        migrations/
        models.py

and you're running the command from the root mysite directory (the same directory manage.py is found in), then you should run your command like so:

python manage.py inspectdb users > myapp/models.py

Greg Kaleka
  • 1,942
  • 1
  • 15
  • 32
  • Iam using the 'run manage.py Task' console of PyCharm and I tried also to give the relative path to the command. But the result is the same. # Unable to inspect table '>' # The error was: (1146, "Table 'mydatabase.>' doesn't exist") # Unable to inspect table 'care/models.py' # The error was: (1146, "Table 'mydatabase.care/models.py' doesn't exist") Process finished with exit code 0 – Perino Feb 13 '20 at 07:02
0

do not use django-admin inspectdb as django will not "know" the settings you are using.

Just use python manage.py inspectdb yourtable

csandreas1
  • 2,026
  • 1
  • 26
  • 48