0

So I'm having a bit of an issue here. I'm pretty new to Flask and web dev in general. I'm trying to add a new item in a database with SQLAlchemy and for some reason it's not working. This is my first practice project and it's extremely basic. You're likely going to find many issues; things that break best practice rules. I understand that, but please be indulgent. I'm just trying to get the very basics to work, then I'll improve the code.

There is the database model:

class TaskType(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    name = db.Column(db.Text(255))
    color_code = db.Column(db.Text, default='NULL')
    creation_timestamp = db.Column(db.DateTime, default=datetime.utcnow())
    last_update_timestamp = db.Column(db.DateTime, default='NULL')

    def __repr__(self):
        return '<Task type %r>' % self.id

Here is the code for the app.route code:

@app.route('/options/tasks-types', methods=['POST', 'GET'])
def options_tasks_types():

    global active_user_id

    if active_user_id != 0:

        if request.method == 'POST':

            task_type_name = request.form['name']
            task_type_color = request.form['color_code']

            timestamp = datetime.now().replace(microsecond=0)

            new_task_type = TaskType(user_id=active_user_id,
                                     name=task_type_name,
                                     color_code=task_type_color,
                                     creation_timestamp=timestamp,
                                     last_update_timestamp='NULL')

            try:
                db.session.add(new_task_type)
                db.session.commit()
                return redirect('/options/task-types')

            except:
                return "The task type could not be created."

        else:
            task_types = TaskType.query.filter(User.id == active_user_id).order_by(TaskType.name.desc()).all()
            return render_template('options_tasks_types.html', task_types=task_types)

    else:
        return "You were disconnected."

Here is the HTML form:

{% extends 'base.html' %}

{% block head %}
<title>Task Manager</title>
{% endblock %}

{% block body %}
<div class="content" style="padding: 50px">
    <h1>Task types</h1>

    <table id="tasks_types">
        <tr>
            <th>Name</th>
            <th>Color</th>
        </tr>
        {% for task_type in task_types %}
            <tr>
                <td>{{ task_type.name }}</td>
                <td>
                    <div class="color-box" style="background-color: {{ task_type.color_code }} ;"></div>
                    </div>
                </td>
                <td>
                    <a href="">Edit</a>
                    <br>
                    <a href="">Delete</a>
                </td>
            </tr>
        {% endfor %}
        <tr>
            <form action="{{ url_for('options_tasks_types') }}" method="POST">
                <td>
                    <input type="text" name="name" id="name" placeholder="Task type name">
                </td>
                <td>
                    <input type="text" name="color_code" id="color_code" placeholder="Color code">
                </td>
                <td>
                    <input type="submit" value="Add new task type">
                </td>
            </form>
        </tr>
    </table>
</div>
{% endblock %}

For some reason I keep getting the Except message "The task could not be created". Unfortunately I can't really know why the exception is even happening. Is there a way to get a more detailed error message?

I've tried removing the try and except statements to just get the Flask error message, but it's not exactly detailed...

All I get is:

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

I have a feeling that the issue is probably coming from my model and that an argument for one of the columns is preventing the TaskType item to be added to the database, but I can't figure out what it is.

Could someone help?

3
  • Could you post the full stack trace, you can use debug=True in app.run() to see the stack trace Commented May 13, 2020 at 2:31
  • @CodeYard I got this, maybe it can help. sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input. The date is formatted this way (2020, 5, 12, 22, 44, 36) Commented May 13, 2020 at 2:45
  • "How do I proceed to get more details as to why it's not working?" — don't use bare except:, it catches everything, including SystemExit and interrupts. It is sometimes used to handle cleanup (rollback), but in that case you would always re raise the original exception instead of supressing it. Catching exceptions when you don't actually handle the error is usually not a good idea, and hinders debugging as seen here. Commented May 13, 2020 at 6:54

1 Answer 1

1

You are assigning a column in your table last_update_timestamp to 'NULL'. It only accepts DateTime objects.

From my understanding you are passing in 'NULL' it has quotes. So it is a string. Try passing in a DateTime object or null

Sign up to request clarification or add additional context in comments.

1 Comment

Yeak ok it was the 'NULL' that caused the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.