How can I find out the desciption of the database with sqlalchemy much like django's
sqlall command .
$ python manage.py sqlall
Can I run something similar to sqlall in turbogears admin or with sqlalchemy with which I can get the create table or something of all the current models.
You want to look at table reflection as well as the inspector.
Related
I see some issues in my database while updating my model in Django. it only shows 12 fields!
when I added one more field to it and then run makemigrations command, it does not show any changes.
I'm using MySQL database init. is there anything in the Django model like we can only define some fields or we can define as much as we need?
You should be a able to create as my fields as you want. I recommend deleting all files in your migrations folder, except __int__.py file and try python manage.py makemigrations and python manage.py migrate again.
If this doesn't work, also delete the db.sqlite3 file. This usually works for me!
So I'm working on this Django project, with MySQL as DB engine. I made some sort of mistake, and now I have to drop a table I accidentally created. So I'm trying to access the DB through command line, but cannot figure out how.
Could anyone help? Or is there a better way of dropping a table in MySQL DB?Thanks in advance.
Use the dbshell command
python manage.py dbshell
then while in the shell, depending on what database you are using, you type the command to show tables to identify the table you want to drop.
still in the shell, you can use SQL command to drop the table
DROP TABLE shop_brand;
source => How can i delete database table in django?
You can use python manage.py dbshell.
I created a fixture with this command:
python manage.py dumpdata --natural-primary --natural-foreign -e admin -e contenttypes -e auth.Permission --format=json --indent=2 --all > gc_core/fixtures/initial_data.json
Then I tried to test it on my other machine. So I deleted my database and run migrations there:
./manage.py migrate
Then I tried to populate my database with that fixture using loaddata:
./manage.py loaddata gc_core/fixtures/initial_data.json
Then Django raises this error:
django.db.utils.OperationalError: Problem installing fixtures: no such column: REFERRED.id
I have no idea what this means. Should I have a column somewhere named REFERRED.id?
There is no such column in my fixture nor in the database and I didn't defined such model in any of my apps.
So what is REFERRED.id and how do I fix it?
I'm using Django 1.10, Python 3.5.2, SQLite. The fixture I'm trying to load is in JSON format.
Thank you in advance.
I stumbled upon this question when I encountered the same issue.
Here is a possible answer that is likely just a theory but I will explain why.
Django is having trouble loading relationships with primary keys that are not auto indexes on SQLite databases.
I am encountering this issue because the test database strategy we employ is by using an SQLite database instead of the production like Postgres database. I have been breaking my head over the exact same error all day. When I switched to Postgres for the test database as well, it ended up not being an issue. There might be a bigger problem at large here. Hopefully someone else can provide a more clear answer.
I am using the same version of Django and Python 2.7
If possible, could you try moving the data into a Postgres equivalent and load it. If that does work, it means your data is not wrong, and the relationship SQLite and Django is having issues.
Background info:
I am quite beginner with django, better equipped with python and quite beginner in mysql (and thus mariaDB) as well.
I work in Windows 7 environment with the following versions:
django 1.9.1
mariaDB 10.0.20
Python 3.4.4
mysql-connector\Python 2.1.3 for Python 3.4
Goal:
I am trying to build mariaDB database and web based user interface for it with django. I have built the database beforehand in mysql and am now trying to create the django project using this database as legacy database.
Problem:
I have retrieved the models to my django project as follows:
python manage.py inspectdb > models.py
and it worked fine. Checked the models.py, as well, and they seemed as they should.
Now, if I try to migrate to create the tables based on django to my legacy db as follows:
python manage.py migrate
I keep getting plenty of nested errors last one being:
django.db.migrations.exceptions.MigrationsSchemaMissing: Unable to create the django_migrations table (Table 'mydb'.'django_migrations' already exists. Please DISCARD the tablespace before IMPORT.)
I figured the table was left in my database folder from previous attempt, but removing it does not help. Even if I check from the database command prompt that the only tables left are native to my database, this same error message pops up still. At some point I wondered if there is some issue with the connector I am using as it is not the recommended one (mysqlclient).
Somewhere, I found that the migration would not even be required. This would obviously save me, but I have no idea, if that is a solution I can go for. Ideally the database would be managed later online and users with admin accounts could add entries there. Otherwise, however the database should be just returning the data.
EDIT: Fixed the error message the python manage.py inspectdb > models.py line
EDIT 29.1.
Running the migration as:
python manage.py migrate --fake
results in the same error.
The issue persists even after removing the django project and starting it fresh.
EDIT v2 29.1.
There is only one of the required table files for the django_migrations table within the database folder. There should be django_migrations.ibd AND django_migrations.frm. However, after running the migration, there is only one file: django_migrations.ibd.
There was something similar going on here, with different file
mysqldump problems with restore error: 'Please DISCARD the tablespace before IMPORT'. It is well explained in the chosen answer.
I think this is related to my issue, but cannot figure out what could help and what to do with this.
EDIT v3 29.1.
And finally I noticed the file giving the django error. It is django's migration recorder at C:\Python34\lib\site-packages\django\db\migrations\recorder.py on line 59.
def ensure_schema(self):
"""
Ensures the table exists and has the correct schema.
"""
# If the table's there, that's fine - we've never changed its schema
# in the codebase.
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
return
# Make the table
try:
with self.connection.schema_editor() as editor:
editor.create_model(self.Migration)
except DatabaseError as exc:
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
Where the last line raising the error is the line 59. Funnily, the function should work, if the table exists. Only thing I can come up with (as in my previous edit), is that indeed for some reason the frm-file is not created during the migration process.
Why that happens is way beyond me.
I basically want to take an existing mysql database structure created and used by a php app (codeigniter framework) and reverse engineer it to a django app. is there some tool to do this? south migrations maybe?
Create a project, and point your settings # your database
Then run
./manage.py inspectdb
This will print out a python models file for the DB you're pointing at
You can output this to a file by doing something like
./manage.py inspectdb > models.py
And then you can move the file to the most suitable location, and edit it as needed.