Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#23987 closed Bug (fixed)

sqlite backend doesn't always use effective_default()

Reported by: Andriy Sokolovskiy Owned by: Andriy Sokolovskiy
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Problem found here:
https://github.com/django/django/pull/3700#issuecomment-66838368

Look at this line:
https://github.com/django/django/blob/df9f2e41fae68e2079db61e07569fdc89d1d6343/django/db/backends/sqlite3/schema.py#L73

            if field.has_default():
                mapping[field.column] = self.quote_value(
                    self.effective_default(field)
                )

Main is that effective default will be used only when field already has default.
That is not correct, because effective_default decides what default would be used.

The second minor problem is that field.has_default() check is already in effective_default method, so we don't need to call it twice.

Change History (9)

comment:1 Changed 6 years ago by Andriy Sokolovskiy

If it will be reviewed as bug, I'll create patch for it.

comment:2 Changed 6 years ago by Andriy Sokolovskiy

As I can see need to have something like this.
Not sure about ManyToManyField, but without check for ManyToManyField some tests fails.

Index: django/django/db/backends/sqlite3/schema.py
===================================================================
--- django/django/db/backends/sqlite3/schema.py	(date 1417898599000)
+++ django/django/db/backends/sqlite3/schema.py	(revision )
@@ -69,8 +69,8 @@
         # Add in any created fields
         for field in create_fields:
             body[field.name] = field
-            # If there's a default, insert it into the copy map
-            if field.has_default():
+            # Choose default and insert it into the copy map
+            if not isinstance(field, ManyToManyField):
                 mapping[field.column] = self.quote_value(
                     self.effective_default(field)
                 )

comment:3 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:4 Changed 6 years ago by Andriy Sokolovskiy

Owner: changed from nobody to Andriy Sokolovskiy
Status: newassigned

comment:5 Changed 6 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 089047331d972c0ee58d13476fc54f2118bf1359:

Fixed #23987 -- Made SQLite SchemaEditor always use effective_default().

comment:6 Changed 6 years ago by Tim Graham <timograham@…>

In 1690b92b0d53e625ef6623a317149013725015af:

[1.7.x] Fixed #23987 -- Made SQLite SchemaEditor always use effective_default().

Backport of 089047331d972c0ee58d13476fc54f2118bf1359 from master

comment:7 Changed 6 years ago by Tim Graham <timograham@…>

In ac5f2a4ef7b9993502ebc02f487cbb06bfb9bf0a:

Fixed refs #23987 test on Oracle.

comment:8 Changed 6 years ago by Tim Graham <timograham@…>

In 66c0529b3edb40e25713cbf32ea8794befc829ae:

[1.7.x] Fixed refs #23987 test on Oracle.

Backport of ac5f2a4ef7b9993502ebc02f487cbb06bfb9bf0a from master

comment:9 Changed 6 years ago by Tim Graham

Summary: sqlite backend don't always use effective_defaultsqlite backend doesn't always use effective_default()
Note: See TracTickets for help on using tickets.
Back to Top