search_title = 'A' * 11
Post.objects.get_or_create(title=search_title)
Post.objects.get_or_create(title=search_title)
}}}
This code creates two objects because lookup is trying to find non-
truncated title and fails to get one so it creates a new object with a
truncated title. The second call does exactly the same creating another
object.
I don't know if it supposed to work this way or not but this behavior
seems unclear to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
What database are you using? That should fail validation and raise an
exception rather than truncating the title field. This may really depend
on your database type.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:1>
Comment (by zhebrak):
Replying to [comment:1 mdunhem]:
> What database are you using? That should fail validation and raise an
exception rather than truncating the title field. This may really depend
on your database type.
We use MySQL (5.1, 5.5). By default it truncates any given string by
max_length for the sake of not giving errors. It's not obvious though,
especially when using get_or_create.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:2>
* type: Bug => Cleanup/optimization
* component: Database layer (models, ORM) => Documentation
* stage: Unreviewed => Accepted
Comment:
I think there's not much Django can do except to recommend enabling
`STRICT_TRANS_TABLES` to avoid silent truncation in the MySQL database
notes in `docs/ref/databases.txt`. It's enabled by default for MySQL 5.7+.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:3>
Comment (by claudep):
#15940 is related.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:4>
Comment (by claudep):
See also the [https://github.com/adamchainz/django-
mysql/commit/5ea46822c403f85964f3f9aba0b412bcfb167563
`strict_mode_warning` check] in django-mysql.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:5>
* has_patch: 0 => 1
Comment:
A [https://github.com/django/django/pull/6292 possible resolution] by
implementing a database check (partially copied from django-mysql).
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:6>
* needs_better_patch: 0 => 1
Comment:
This would be extending the system check framework in a new direction
(currently it's mostly limited to static code analysis). While I don't see
a big problem with this, I'll raise the alternative of a separate
`testconnections` command as proposed in #24475. Anyway, I left some
comments for improvement on the patch as currently proposed.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:7>
* cc: me@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:8>
Comment (by mdunhem):
Should we also add a note in the documentation?
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:9>
Comment (by collinanderson):
As a cross reference, there are warnings generated when there's
truncation, and they used to be errors, but only in debug mode. #23871
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:10>
* needs_better_patch: 1 => 0
Comment:
I've updated the pull request and separated it in two commits. In the
first one, I've extended the `DatabaseValidation` class to host database-
related checks. I also limited database-related checks to only the
`migrate` command so we don't get those warnings/errors for each check
run.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:11>
Comment (by shaib):
If MySql has already made `STRICT_TRANS_TABLES` the default, I suggest we
do the same. In a few versions, when MySql 5.5 and 5.6 reach EOL, we'll be
there anyway.
This comment is almost orthogonal to the solution of database checks,
which is a generally needed feature. The only point of contact is the
check for `STRICT_TRANS_TABLES` itself, since if we default to it, a user
who explicitly chose otherwise doesn't need to be warned about their
choice.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:12>
Comment (by claudep):
I'm +0 about forcing `STRICT_TRANS_TABLES`. I think the main advantage of
the check over forcing the setting is that we don't load each connection
creation on runtime with the cruft of SELECTing the variable to check its
current state. The other minor advantage is that people are still free to
configure their database at their will, even if I don't see now a use case
for that.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:13>
Comment (by timgraham):
I guess it's a good point about whether we should add the check now that
MySQL is changing the default of `STRICT_TRANS_TABLES`. As Shai said,
presumably going forward then, this check will only trigger for users
configuring the database otherwise, so I wonder if it adds any value
except for users of older versions of MySQL?
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:14>
Comment (by claudep):
I think having the check is a good mean to warn users that their MySQL
configuration (being for an old MySQL or a MySQL with custom config) might
interfere with their Django project running properly. Then, if the choice
is deliberate, they can safely disable the warning.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:15>
Comment (by adamchainz):
It's also valuable for MariaDB users where it's not yet the default.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:16>
Comment (by timgraham):
How does the proposed patch interact with #15940? It's not clear to me
whether this solves the concerns of that ticket or if something else might
need to be done to address it after adding this check?
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:17>
Comment (by claudep):
I think it does. IMHO the most important thing is to make people aware of
the possible truncation issue. What they do next is their business.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:18>
Comment (by timgraham):
Okay. The `docs/ref/databases.txt` note in your patch recommends
`STRICT_TRANS_TABLES` as opposed to `STRICT_ALL_TABLES` or `'sql_mode':
'traditional'` as mentioned in the other ticket. If there's a reason to
prefer one or the other, I didn't see it discussed. Not a MySQL user
myself, just wanted to check about that.
[http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html MySQL SQL mode
reference docs].
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:19>
Comment (by claudep):
I updated the docs to mention various modes, and also added a link to the
MySQL docs. I also accepted `traditional` as an accepted strict mode.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:20>
* needs_better_patch: 0 => 1
Comment:
Left some comments for improvement.
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:21>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:22>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:23>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"f9a2a7db173b53f9cb0cd2d3def80d4eed72631c" f9a2a7d]:
{{{
#!CommitTicketReference repository=""
revision="f9a2a7db173b53f9cb0cd2d3def80d4eed72631c"
Fixed #26351 -- Added MySQL check to warn about strict mode option
Thanks Adam Chainz for the initial implementation in django-mysql.
Thanks Adam Chainz, Tim Graham, and Shai Berger for the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:25>
Comment (by Claude Paroz <claude@…>):
In [changeset:"0d3c616fbb2f49fa7ff6809e5a6777275352b35b" 0d3c616]:
{{{
#!CommitTicketReference repository=""
revision="0d3c616fbb2f49fa7ff6809e5a6777275352b35b"
Refs #26351 -- Added check hook to support database-related checks
Thanks Tim Graham and Shai Berger for the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:24>
Comment (by Tim Graham <timograham@…>):
In [changeset:"93deb1691eb27dc89135511fb0c10e077c8baca7" 93deb16]:
{{{
#!CommitTicketReference repository=""
revision="93deb1691eb27dc89135511fb0c10e077c8baca7"
Fixed #26492 -- Fixed "maximum recursion depth exceeded" migrate error.
A regression introduced in 0d3c616fbb2f49fa7ff6809e5a6777275352b35b;
refs #26351.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26351#comment:26>