#22424 closed Bug (fixed)
Default value for TextField
Reported by: | Vitaly Yakubenko | Owned by: | |
---|---|---|---|
Component: | Migrations | Version: | 1.7-beta-1 |
Severity: | Release blocker | Keywords: | |
Cc: | loic84, denis.cornehl@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Hi! I start testing Django 1.7beta1. I use MySQL database.
I add new TextField into my model like that:
class Article(models.Model): title = models.CharField(max_length=200) pub_date = models.DateField() text = models.TextField() text2 = models.TextField() #new TextField
When I type "python manage.py makemigrations" I am asked to enter default value.
But BLOB/TEXT columns can't have a default value and if i will type something like 'blabla' then after command "python manage.py migrate" i will see error. I think it is bug :)
Change History (19)
follow-up: 2 comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
Replying to loic84:
I can reproduce that adding a TextField() triggers the questioner which is probably confusing, I wonder if we can't just use as default automatically when default=NOT_PROVIDED and empty_strings_allowed=True. Accepting the ticket on this basis.
Yes I think correct is to use "None" as default in this instance.
Regarding the error with
'blabla'
, did you actually type the quotes? That prompt interprets Python, so if you just typeblabla
it'll throw an error because it can't find a variable named "blabla".
Yes, I type the quotes and migration successfully making. I confused commands "migrate" and "magemigrations" in ticked description. I get error when I trying to apply created migration because it contains default value for field that can't have default value in MySQL database :)
comment:3 by , 10 years ago
Loic, I'm not sure using ''
as an automatic default is better than the current situation. Why not give the user the option of entering something else besides that (especially if blank=False
)?
comment:4 by , 10 years ago
Description: | modified (diff) |
---|
Fixed the name of the commands in the ticket description.
I originally assumed this was an issue before the migrate step, but it's indeed an SQL issue due to one-off defaults, Django uses db defaults for these.
The fix for MySQL is to not provide a default for BLOB/TEXT in the SchemaEditor. Although we'll now need to propagate the default value manually.
comment:5 by , 10 years ago
@Nevsky could you check if this solves your problem? https://github.com/django/django/pull/2634
comment:6 by , 10 years ago
Cc: | added |
---|
comment:7 by , 10 years ago
loic84: That PR looks like the right approach to me; will MySQL reject any default value on blob/text columns or just empty ones? Oracle has a weird behaviour where it treats empty strings as NULL on text columns that causes similar issues, but we have a fix for that.
comment:8 by , 10 years ago
MySQL rejects anything even remotely related to a default for blob/text columns (i.e. ALTER TABLE 'table' ALTER COLUMN 'blob' DROP DEFAULT
).
comment:9 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I'll have a look at it and verify the fix.
(atm I get Nevsky's error when adding the column without the fix, with the fix I get another error. But checking it.
comment:10 by , 10 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:11 by , 10 years ago
Update the PR with some comments:
https://github.com/django/django/pull/2634
short version:
PR failing when there are empty or string defaults.
But with fixed everything (in this ticket) seems fine.
comment:12 by , 10 years ago
Cc: | added |
---|
comment:13 by , 10 years ago
Updated the PR to fix the issue reported by @syphar and to incorporate the fix for #22626.
Sadly the handling of bytes/strings is still less than ideal in the many quote_value
:
"
in adefault
will break SQLite and MySQL.- Oracle doesn't support bytes so breaks on
BinaryField
. - Oracle uses
repr
to quotesix.string_types
, which will probably result in an extrau
prefix.
I propose we open a new ticket to deal with this issue separately.
comment:16 by , 10 years ago
Owner: | set to |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Hi @Nevsky,
default
is actually handled at the Python level, not added to the SQL, so that shouldn't be an issue.I can reproduce that adding a
TextField()
triggers the questioner which is probably confusing, I wonder if we can't just use''
as default automatically whendefault=NOT_PROVIDED
andempty_strings_allowed=True
. Accepting the ticket on this basis.Regarding the error with
'blabla'
, did you actually type the quotes? That prompt interprets Python, so if you just typeblabla
it'll throw an error because it can't find a variable named "blabla".