<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saphi8</title>
    <description>The latest articles on DEV Community by Saphi8 (@saphi8).</description>
    <link>https://dev.to/saphi8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1412391%2F1ce8ddd5-5312-493d-94e3-d064c88f7ce0.jpeg</url>
      <title>DEV Community: Saphi8</title>
      <link>https://dev.to/saphi8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saphi8"/>
    <language>en</language>
    <item>
      <title>I've a little problem with Flask-SQLAlchemy</title>
      <dc:creator>Saphi8</dc:creator>
      <pubDate>Mon, 08 Apr 2024 15:37:59 +0000</pubDate>
      <link>https://dev.to/saphi8/ive-a-little-problem-with-flask-sqlalchemy-4j60</link>
      <guid>https://dev.to/saphi8/ive-a-little-problem-with-flask-sqlalchemy-4j60</guid>
      <description>&lt;p&gt;Problem solved !&lt;/p&gt;

&lt;p&gt;I'm currently in highschool, and we have a project : create a website. We're three and my part is the backend, with Flask in python.It is the first time I try to do something like that. The first thing I need to do is create an API for new users, but I can't write things on the database.&lt;/p&gt;

&lt;p&gt;Here is the final error :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) attempt to write a readonly database 
[SQL: INSERT INTO user (id, mdp, accents) VALUES (?, ?, ?)]
[parameters: ('saphi', '$2b$12$0McLhda54LEQtkg8QHxff.f.rJDADQ.sDsAGfvXHy8vhl4H9wE0y6', "{'': ['', '']}")]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the part where I Initialize the database : (thoth-edu/thoth-edu/Backend/appInit.py)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////home/ubuntu/thoth-edu/database/data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
CORS(
    app,
    resources={
        r"/.*": {"origins": ["https://thoth-edu.fr", "https://professeur.thoth-edu.fr"]}
    },
)
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.String, unique=True, nullable=False, primary_key=True)
    mdp = db.Column(db.String, nullable=False)
    accents = db.Column(db.String)



class Eval(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    cheminJSON = db.Column(db.String)
    cheminCSV = db.Column(db.String)
    idProf = db.Column(db.String, db.ForeignKey("user.id"), nullable=False)


class Acces(db.Model):
    id = db.Column(db.String, unique=True, nullable=False, primary_key=True)
    nom = db.Column(db.String, nullable=False)
    dateDeb = db.Column(db.String, nullable=False)
    dateFin = db.Column(db.String, nullable=False)
    modele = db.Column(db.String, db.ForeignKey("eval.id"), nullable=False)



with app.app_context():
    try:
        db.create_all()
        print("Tables created successfully.")
    except Exception as e:
        print("An error occurred while creating tables:", e)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What my API look like : (thoth-edu/thoth-edu/Backend/main.py)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import libraries
from flask import request
import json

# Import routes (and other modules)
import routes as r
from appInit import app

@app.route("/user/login", methods=["POST"])
def connexion():
    data = request.get_json()
    return r.user.login(data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And what r.user.login look like: (thoth-edu/thoth-edu/Backend/routes/user/login.py)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import libraries
from flask import jsonify
import sqlite3

# Import app
from appInit import db, User, bcrypt


def signup(data):
    # { "id" : "Bob" ; "mdp" : "mdp" ; "accents" : "é" }
    newUser = User(
        id=data["id"],
        mdp=bcrypt.generate_password_hash(data["mdp"]).decode("utf-8"),
        accents=str(data["accents"]),
    )

    user = User.query.filter_by(id=data["id"]).first()

    if user == None:
        db.session.add(newUser)
        db.session.commit()
        return (jsonify({"message": "True"}),)

    if user.id == data["id"]:
        return (jsonify({"message": "False"}),)

    db.session.add(newUser)
    db.session.commit()

    return (jsonify({"message": "True"}),)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what I tried :&lt;br&gt;
 -&amp;gt; Giving rwx permissions for everyone&lt;br&gt;
 -&amp;gt; Install sqlite with sudo apt install sqlite3 Nothing worked, and I didn't found anything else to help me&lt;/p&gt;

&lt;p&gt;Note that I’m trying this out in a VM (running Debian 12 with KDE), and that the files of the project are taken from my computer (and accessed through the shared files functionality of VirtualBox).&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>help</category>
    </item>
  </channel>
</rss>
