Python Forum

Full Version: sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying learn python and flask by building a simple web application.

The user will enter the product details in web application form which will be inserted in to postgres table. When I enter the product details and click submit i see below error

Error :
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "product_code" violates not-null constraint
DETAIL: Failing row contains (null, null, null, null, null).


class BkpFulfilmentProductProvider(db.Model):
    """ Databae Model for FUlfilment Product Provider"""
    __tablename__ = 'bkp_fulfilment_product_provider'

    product_code = db.Column(db.String(100), primary_key=True)
    fulfilment_provider_code = db.Column(db.String(100))
    is_primary = db.Column(db.Boolean)
    on_hand_stock = db.Column(db.Integer)
    on_order_stock = db.Column(db.Integer)


@app.route('/')
def fulfilment_view():
    return render_template('Ful_Product_Provider.html')


@app.route('/Fulfilment', methods=['POST'])
# if request.method == "POST":
def fulfilmentproductprovider():
    #  from models import BkpFulfilmentProductProvider
    if request.method == 'POST':
        prod_cd = request.form.get('product_code')
        ful_provider_cd = request.form.get('fulfilment_provider_code')
        primary = request.form.get('is_primary')
        stock_in_hand = request.form.get('on_hand_stock')
        ordered_stock = request.form.get('on_order_stock')
        fulfil = BkpFulfilmentProductProvider(product_code=prod_cd, fulfilment_provider_code=ful_provider_cd,
                                          is_primary=primary, on_hand_stock=stock_in_hand, on_order_stock=ordered_stock)
        db.session.add(fulfil)
        db.session.commit()
    return render_template('Ful_Product_Provider.html')

[python]
if __name__ == '__main__':
app.run(debug=True)
[/python]

Can you please advice on the error

Thank
kt
check your submitted data, somehow you are getting an empty row or at least null primary key (product_code)
Thanks for the response Larz60+,

I have now fixed the sqlalchemy.exc.IntegrityError issue, but when i enter the values through web form they are not getting inserted into DB, instead they are written to the browser links as below

http://127.0.0.1:5000/?prod_cd=tere&ful_...d_stock=31

i'm using below html code


<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Fonts -->
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css">


<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<title>Fulfilment Product Provider</title>
</head>
<body>

<form class="my-form">
<div class="cotainer">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Enter Fulfilment Product Provider Codes </div>
<div class="card-body">
<form name="my-form" action="/Fulfilment" method="post" >
<div class="form-group row">
<label for="PRODUCT_CODE" class="col-md-4 col-form-label text-md-right">PRODUCT_CODE</label>
<div class="col-md-6">
<input type="text" id="PRODUCT_CODE" class="form-control" name="prod_cd">
</div>
</div>

<div class="form-group row">
<label for="FULFILMENT_PROVIDER_CODE" class="col-md-4 col-form-label text-md-right">FULFILMENT_PROVIDER_CODE</label>
<div class="col-md-6">
<input type="text" id="FULFILMENT_PROVIDER_CODE" class="form-control" name="ful_provider_cd">
</div>
</div>

<div class="form-group row">
<label for="IS_PRIMARY" class="col-md-4 col-form-label text-md-right">IS_PRIMARY</label>
<div class="col-md-6">
<input type="text" id="IS_PRIMARY" class="form-control" name="primary">
</div>
</div>

<div class="form-group row">
<label for="ON_HAND_STOCK" class="col-md-4 col-form-label text-md-right">ON_HAND_STOCK</label>
<div class="col-md-6">
<input type="text" id="ON_HAND_STOCK" class="form-control" name="stock_in_hand">
</div>
</div>

<div class="form-group row">
<label for="ON_ORDER_STOCK" class="col-md-4 col-form-label text-md-right">ON_ORDER_STOCK</label>
<div class="col-md-6">
<input type="text" id="ON_ORDER_STOCK" class="form-control" name="ordered_stock">
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>

Thanks
Kt
I use sqlalchemy all the time, but only occasionally with flask.
I suggest that you run this tutorial: https://blog.miguelgrinberg.com/post/the...v-database