-1

when i try to run this code in python i get the type errors File "/home/codio/workspace/MyProject/app.py", line 8, in class Book(db.Model): File "/home/codio/workspace/MyProject/app.py", line 15, in Book tradePrice = db.Column(db.Integer(length=10), nullable=0) TypeError: object() takes no parameters I am new to flask and im dont understand whats causing this

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy # database library for flask
# using render_template to have the html code in different files
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///books.db' #confgurating a database
db = SQLAlchemy(app)

class Book(db.Model):
    barcode = db.Column(db.String(length=12), nullable=0, primary_key=1)
    title = db.Column(db.String(length=50), nullable=0, unique=1)
    author = db.Column(db.String(length=50), nullable=0)
    publicationDate = db.Column(db.String(length=10), nullable=0)
    quantity = db.Column(db.Integer(), nullable=0)
    description = db.Column(db.String(length=1000), nullable=0)
    tradePrice = db.Column(db.Integer(length=10), nullable=0)
    retailPrice = db.Column(db.Integer(length=10), nullable=0)

    
@app.route("/login") # home page
def login_page():
        return render_template("login.html")
    
    
@app.route("/home")
@app.route("/")
def home_page():
              return render_template("home.html")
@app.route("/stock")
def stock_page():
        return render_template("stock.html", books=books)
davidism
  • 121,510
  • 29
  • 395
  • 339
  • What does it mean for an `Integer` to have a length of 10? That class is ignoring the unexpected `length` keyword argument and passing it up the chain. Eventually, it gets passed to `object`, resulting in your error. – chepner Nov 22 '21 at 14:21
  • That's a database thing, like the difference between `long` and `double` – Paul Becotte Nov 22 '21 at 14:22
  • @PaulBecotte `Integer` is just a wrapper for `int`, however the DB defines that. It's not something you can configure here. – chepner Nov 22 '21 at 14:24
  • Yes, but it can be configured elsewhere (mssql has an integer type that does that for example). For the purpose of a beginner though- just remove it, not necessary. – Paul Becotte Nov 22 '21 at 14:26
  • actually, my memory was wrong- mysql vs sql server, and `display_width` versus `length`. Sorry! – Paul Becotte Nov 22 '21 at 14:31

1 Answers1

0

Firstly after create a model you shoul run code below at terminal and then you can create property or classmethod or something.

python manage.py db init # flask db init
python manage.py db migrate # flask db migrate
python manage.py db upgrade # flask db upgrade

And change your code like below code:

db.Column(db.Integer(256), nullable=False) # dont use length

And you should use app.run() - To start your project at local

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy # database library for flask
# using render_template to have the html code in different files
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///books.db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)

class Book(db.Model):
   barcode = db.Column(db.String(length=12), nullable=0, primary_key=1)
   title = db.Column(db.String(length=50), nullable=0, unique=1)
   author = db.Column(db.String(length=50), nullable=0)
   publicationDate = db.Column(db.String(length=10), nullable=0)
   quantity = db.Column(db.Integer(), nullable=0)
   description = db.Column(db.String(length=1000), nullable=0)
   tradePrice = db.Column(db.Integer(length=10), nullable=0)
   retailPrice = db.Column(db.Integer(length=10), nullable=0)
 
@app.route("/login") # home page
def login_page():
        return render_template("login.html")
    
    
@app.route("/home")
@app.route("/")
def home_page():
              return render_template("home.html")
@app.route("/stock")
def stock_page():
        return render_template("stock.html", books=books)


if __name__ == "__main__":
    # port = app.config.get("PORT", 8080)
    app.run(host="0.0.0.0", port=5000, debug=True)

And you should use db.create_all()

If you want to search you should check the link: SQLAlchemy create_all() does not create tables

Ayse
  • 576
  • 4
  • 13