Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String to dates for relations #265

Merged
merged 1 commit into from Dec 1, 2013
Merged

String to dates for relations #265

merged 1 commit into from Dec 1, 2013

Conversation

@bildja
Copy link
Contributor

@bildja bildja commented Nov 23, 2013

When updating the related nested models with the date fields, they (the date fields) don't convert to the datetime python objects and it raises an exception. For example, we have such models:

class Computer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    purchase_time = db.Column(db.DateTime)
    notes = db.relationship('Note')


class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.datetime.now)
    computer_id = db.Column(db.Integer, db.ForeignKey('computer.id'))

We send such PUT request to /api/computer/18:

{
    "id":18,
    "name":"Galina.",
    "notes": [
        {
            "text":"ew",
            "computer_id":18,
            "created_at":"2013-11-23T13:18:57"
        }
    ],
    "purchase_time":"2013-11-09T14:55:23"
}

The purchase_time field is being perfectly parsed and converted to the datetime python object in the .patch() method of view, but the created_at field in notes raises an exception:
StatementError: SQLite DateTime type only accepts Python datetime and date objects as input. (original cause: TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.) 'INSERT INTO note (text, created_at, computer_id) VALUES (?, ?, ?)' [{'text': u'test text', 'created_at': u'2013-11-23T13:18:57', 'computer_id': 18}]

When updating the related nested models with the date fields, they (the date fields) don't convert to the `datetime` python objects and it raises an exception. For example, we have such models:

```
class Computer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    purchase_time = db.Column(db.DateTime)
    notes = db.relationship('Note')


class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.datetime.now)
    computer_id = db.Column(db.Integer, db.ForeignKey('computer.id'))
```

We send such PUT request to /api/computer/18:
```
{
    "id":18,
    "name":"Galina.",
    "notes": [
        {
            "text":"ew",
            "computer_id":18,
            "created_at":"2013-11-23T13:18:57"
        }
    ],
    "purchase_time":"2013-11-09T14:55:23"
}
```

The `purchase_time` field is being perfectly parsed and converted to the `datetime` python object in the `.patch()` method of view, but the `created_at` field in `notes` raises an exception:
`StatementError: SQLite DateTime type only accepts Python datetime and date objects as input. (original cause: TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.) 'INSERT INTO note (text, created_at, computer_id) VALUES (?, ?, ?)' [{'text': u'test text', 'created_at': u'2013-11-23T13:18:57', 'computer_id': 18}]`
jfinkels added a commit that referenced this issue Dec 1, 2013
String to dates for relations
@jfinkels jfinkels merged commit e8b0140 into jfinkels:master Dec 1, 2013
1 check passed
Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants