Home > front end >  I got an error from SQL while working on a project in flask
I got an error from SQL while working on a project in flask

Time:08-19

i import sql in a flask project i am building i got an error and could not find my way through. though the project was working before i imported sql below is my python code:

**from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
db = SQLAlchemy(app)
class Item(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(lenght=30), nullable=False, unique=True)
    price = db.Column(db.Integer(), nullable=False)
    barcode = db.Column(db.String(lenght=12), nullable=False, unique=True)
    description = db.Column(db.String(lenght=1024), nullable=False, unique=True)
@app.route('/')
@app.route('/home')
def home_page():
    return render_template('home.html')
@app.route('/market')
def market_page():
    items = [
        {'id': 1, 'name': 'phone', 'barcode': '893212299897', 'price': 500},
        {'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900},
        {'id': 3, 'name': 'keyboard', 'barcode': '231985128446', 'price': 150}
    ]
    return render_template('market.html', items=items)**

and below is the error I received:

**C:\Users\SUCCESS-AKINYEMI\Desktop\New folder\flaskk>flask run
C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_sqlalchemy\__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 1047, in main
    cli.main()
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 911, in run_command
    raise e from None
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 897, in run_command
    app = info.load_app()
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 308, in load_app
    app = locate_app(import_name, name)
  File "C:\Users\SUCCESS-AKINYEMI\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 218, in locate_app
    __import__(module_name)
  File "C:\Users\SUCCESS-AKINYEMI\Desktop\New folder\flaskk\nin.py", line 7, in <module>
    class Item(db.Model):
  File "C:\Users\SUCCESS-AKINYEMI\Desktop\New folder\flaskk\nin.py", line 9, in Item
    name = db.Column(db.String(lenght=30), nullable=False, unique=True)
TypeError: String.__init__() got an unexpected keyword argument 'lenght'**

please help me interpret and a solution thanks.

CodePudding user response:

The sqltypes String object has no passable argument named lenght (it's propably a typo). If you want to declaring a String Column with a maximum of 30 characters, just use db.String(30) or db.String(length=30)

CodePudding user response:

    name = db.Column(db.String(lenght=30), nullable=False, unique=True)
length has been misspelled
  • Related