"Error: near "Documents": syntax Error" - sqlite3/Django - django

I am trying Chapter 6 from Django book.
After changing some fields to blank=True, and null=True, we are told to update the table using dbshell.
When I type: python manage.py dbshell, the following error appears:
C:\Users\Rafa\Documents\Python\book>python manage.py dbshell
Error: near: "Documents": syntax error.
I tried installing sqlite3.exe in my book directory and running directly from there. I still get the same error anytime I try to run a command. For example, once on the sqlite3 shell (opening executable file):
sqlite>ALTER TABLE books_book ALTER COLUMN publication_date DROP NOT NULL;
Error: near "ALTER": syntax error.
I know the commands may be wrong, but I can try anything and it will always point the same error and show the first word of the command in the "".
Any help?

SQLite has very limited ALTER TABLE support.
You have to drop and recreate the table.

As the error message suggests, there is a syntax error somewhere. You should check (or post here) the parts which describe Documents class for some syntactic mistakes :)

I realize now that Django does not support database migrations very well. Once you do the initial migration and the original table is set, it is impossible to add or modify fields with Django only. You need a database migration schema like South.

Related

Error while working with two databases in Django: sqlite3.IntegrityError: NOT NULL constraint failed: wagtailcore_page.draft_title

I'm working on a Django Project with Wagtail which uses two databases. The first one is the standard sql lite database for all django models (called db_tool.sqlite3), the other one is also sql lite but for a wagtail integration (called db.sqlite3).
I wanted to migrate to the db_tool.sqlite3 with the following command
python manage.py make migrations
python manage.py migrate --database db_tool
but now I get the following error message regarding wagtail, which I never got before.
django.db.utils.IntegrityError: NOT NULL constraint failed: wagtailcore_page.draft_title
First of all: I don't understand this, because I named the db_tool in particular and I wonder, why the wagtail integration raises an error when I try to migrate to db_tool.
Second: I see no particular field at my wagtail-pages called draft_title and I don't have any draft page at the moment.
Third: the error message also relates to a migration file of wagtail that can be found in the side-packages (see below). So maybe this is the root of the error, but I don't understand the correlation to the other error message, because since now it worked fine and I changed nothing exept of some content of my wagtail pages.
File "C:\Users\pubr\.conda\envs\iqps_web\lib\site-packages\wagtail\core\migrations\0001_squashed_0016_change_page_url_path_to_text_field.py", line 23, in initial_data
root = Page.objects.create(
The wagtail version I use here is wagtail 2.15.2 and I haven't updated it since I started the project...
Due to the fact, that my wagtail-database has the name of the default django-database, could it be possible, that I accidentally tried a migration which was ment for the tool_db.sqlite3 without naming it in the migrate-command and caused this error by doing that?
So I would be very grateful if anyone knows, where the error comes from, or at least, what I could try out to fix it...
Kind regards and thank you!
It isn't clear to me if your database is currently broken or not. Hopefully not, but if it is, please take a back up of each before doing anything else.
This does sound like you might have been trying to operate in the wrong database. Do you have DATABASE_ROUTERS configured? I think that might help you prevent code from one app from getting introduced into the wrong database. The example in the Django docs is mostly focused on read replicas but should be adaptable to your situation: https://docs.djangoproject.com/en/4.1/topics/db/multi-db/#an-example
If your databases are in an incorrect state, start by looking at the django_migrations file in each and then carefully pruning the messed up one until you get back to the separation you have been enforcing.

what is no such column: REFERRED.number?

I'm trying to load a fixture and it gives me:
django.db.utils.OperationalError: Problem installing fixtures: no such column: REFERRED.number
Unfortunately, I cannot show the json, because there is all kind of private stuff in there, but I was hoping someone might explain to me what the REFERRED might mean. What kind of error am I looking for?
I made a few migrations and now the DB is out of wack. So the json is slightly off to the DB. There are multiple things called number though. Referred sounds it's some kind of foreignkey error!? Can you give me any hint what to look for?
I had a similar issue and found out what REFERRED was by adding print(query) in django/db/backends/sqlite3/base.py#L297. That showed me all the queries django was running against my sqlite3 database.
In my case, loaddata was not finding the field id (the primary key field that is default in django) for a model in which I had set primary_key=True to one of its fields. Eventhough django was not automatically generating the id field (correct behaviour), loaddata kept looking for the id field. A possible solution would be to add --natural-primary option, but that didn't work for me ATM.
Ref: https://docs.djangoproject.com/en/1.11/topics/serialization/#topics-serialization-natural-keys

How to get Django Report Builder working properly?

Documentation provided by Burke isn't very thorough and there isn't a lot of information on Django Report Builder yet, not even youtube videos. Does anybody have any information on how to get work Report Builder Working in Django? I currently keep getting this error message:
(1146, "Table 'epic_test2.report_builder_report' doesn't exist")
I've installed it, but I can't create reports yet and the documentation I do have isn't very helpful. Any advice would be great! Thanks!
UPDATE TO POST/ 5.12.15 -
I fixed the first issue, but now I have a new error. :) Yay. It's a Field Error
"Cannot Resolve keyword 'name' into field. Choices are: app_label, id, logentry, model, permission, report.
ANY clues for this one would be helpful... going into third hour of troubleshooting. :)
Thanks!
~Heather
Sounds like you may need to run a migrate to create the Report Builder tables required.
python manage.py migrate
I would suggest you do a full search in your code for "name", something like: grep -R "name" ., and see where it's defined, is it a valid field in models.py, etc.
I've had similar error before, turned out, I updated my variable name from something like "name" to "new_name" in my model. The way report-builder works, it saves column (field) name in the database. If it's an old field name, there will be error. Luckily this is easy to fix: go to report-builder UI, remove the row for that column, then add the column back, then the new field name would be saved.
Hope this helps.

Django South Migration introspect_test

I'm trying to do a simple migrate and I'm getting the error
django.db.utils.DatabaseError: (1050, "Table 'introspect_test' already exists").
When looking at the actual MySQL database, I see no table called introspect_test nor do I have such a table defined in any Django model. A little bit of Google-Fu tells me that this introspect_test has something to do with foreign key constraints and South's error checking... or something.
I think this whole problem originated form my own error - while in the middle of executing a python manage.py migrate app_name I accidently hit CTRL-C and, thus, stopped the process.
I guess the question is: how do I get rid of this mysterious introspect_test so that I can migrate normally?
Thanks
Ugh, silly me... what I didn't realise is that the table introspect_test is alls caps and, thus, isn't sorted alphabetically. It turns out that, due to a small laptop screen, doing show tables; listed all the tables but cut off the top few. Since the table in question is labelled as INTROSPECT_TEST it is at the top.
I did a quick drop table INTROSPECT_TEST and then migrated normally.
Fixed.

DatabaseError: value too long for type character varying(100)

I have a Django web site running a mini CMS we've built internally years ago, it's using postgresql. When saving a simple title and a paragraph of text I get the following error:
value too long for type character varying(100)
The weird thing is, not a single column is varying(100) they are all 200 or 250, even the default Django ones have been changed from the 100 to 200 due to a re-opened ticket mentioned here
Does anyone know of a solution to this problem?
I can bet money you have a models.SlugField without length set. The default length is 50 characters, most likely it's not enough for your use case.
Change it to models.SlugField(max_length=255) and migrate your database schema.
I also had this problem when using a filefield and was scratching my head for a while. Of course the default FileField instances are created with a 100 character limit.
https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
This is an error message from Postgres and not django.
You seem to have changed the length of the field in the models.py, but that doesn't change the database length which was created when you did a manage.py syncdb.
You have to alter the length of the field in the database, directly.
Django 2.1
I encountered this problem while switching from sqlite3 to postgresql.
Remove the migration files in each app's migrations folder except __init__.py
Then re-run migration
(venv)myapp$python manage.py makemigrations
(venv)myapp$python manage.py migrate
(venv)myapp$python manage.py runserver
I had a similar problem with django-autoslugfield
I was using a similar package and then switched over to django-autoslugfield
I was getting this error:
value too long for type character varying(50)
despite the fact that my models.py had:
slug = AutoSlugField(max_length=255, populate_from='name', unique=True)
and in my db the it the type was
character varying 255
once i remove max_length=255 from the field i.e.
slug = AutoSlugField(populate_from='name', unique=True)
then it worked fine
i went through this same error. and when i made changes in the modele, i kept having the same error.
Here is how i fixed it.
It might be necessary to skip few migrations for the program to only use the migration where changes have been made for the CharField max_lenght.
for that you have to run
python manage.py showmigrations
to see which migrations have not been made.
then you skip them until you get to the last with the command
python manage.py migrate <app> 000_migration_number --fake
I realize the question is already answered but for others that come here when looking for the error message:
In my case the problem was that my table name exceeded 50 characters. Apparently this is not allowed. Changing the table name solved the problem.
Read more here: https://code.djangoproject.com/ticket/18959
Michael Samoylov's answer pointed me in the right direction. I had the same error come up, except it was with a FileField.
Fields have a max_length, even if you have not explicitly set a max_length. Increase the value so that your data fits to avoid the error.
In my case, the data was too large to reasonably store in the database. I resorted to saving my file to disk, then saving the file path in the database.
I had this problem when I wanted to set max_length to a lower value on a FileField.
Interestingly, the error message states value too long but in my case the value was too short.
The solution was to set back the max_length to its old value. It's quite weird that the migration couldn't be done.
I also had to delete the wrongly generated migrations files and rerun python manage.py makemigrations and python manage.py migrate.
If it's not SlugField, FileField, or any other field mentioned here--scroll back to where the migration got stuck in the terminal. For me it was AddField
Good talk.
First, try setting max_length to something reasonable on all applicable field types in your model.
For example: MyText = models.CharField(max_length=2000)
If you don't set a max_length, your database might be applying a default max_length, shorter than the length of your input data, causing your value too long for type character error.
If that doesn't work and you started in SQLite and changed databases to PostgreSQL, the previous migrations from SQLite might be interfering with the new PostgreSQL migrations.
Go into the migrations folder for your project and delete the old migration files to get a fresh start. Then try makemigrations and migrate again :)
predefined fields in model.py creates the problem. Extend it to desired length, i think problem will be resolved.
For the FileField and the ImageField, from the Django docs:
FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.
The value can be 100 or 25 or 17 whatever , the reason behind that is models , search where you have added that particular length(17,25) and you will get the bug ! ,
You are trying to impose a field length more than mentioned one.
This solved my issue , I hope it will help you too