Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy Numeric Type

I’m making a Flask-SqlAlchemy app. In one model I want to store geo code data as a lat/long.

From reading other posts, I should use Numeric not Float.

The documentation for type Numeric lists several arguments available:

precision=None, asdecimal=False, decimal_return_scale=None

So I tried adding a column as

db.Column(db.Numeric, precision=8, asdecimal=False, decimal_return_scale=None) — as the equivalent of DECIMAL(10,8)

Which returned an error of:

 "Unknown arguments passed to Column: " + repr(list(kwargs)))

sqlalchemy.exc.ArgumentError: Unknown arguments passed to Column: ['asdecimal', 'decimal_return_scale', 'precision']

If I use db.Column(db.Numeric), I’m able to create the tables and write to it.

Would someone explain to me if there is a syntax error in what I passed here: db.Column(db.Numeric, precision=8, asdecimal=False, decimal_return_scale=None) — as the equivalent of DECIMAL(10,8)

What is the proper way to utilize/define a Numeric type column.

like image 411
manisha Avatar asked Sep 09 '16 22:09

manisha


People also ask

Can I use SQLAlchemy in Flask?

Flask-SQLAlchemy is a Flask extension that makes using SQLAlchemy with Flask easier, providing you tools and methods to interact with your database in your Flask applications through SQLAlchemy. In this tutorial, you'll build a small student management system that demonstrates how to use the Flask-SQLAlchemy extension.

What is the difference between Flask-SQLAlchemy and SQLAlchemy?

One of which is that Flask-SQLAlchemy has its own API. This adds complexity by having its different methods for ORM queries and models separate from the SQLAlchemy API. Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult.

What is PickleType?

PickleType. Holds Python objects, which are serialized using pickle.

What is nullable SQLAlchemy?

From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it's rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements.


2 Answers

You can also do

db.Column(db.Numeric(10,2))

Where the first element is the quantity of integer places and the second argument represents the amount of decimal places.

like image 81
Julio Aguilar Avatar answered Oct 19 '22 15:10

Julio Aguilar


You should add the extra arguments to db.Numeric, and not to db.Column:

db.Column(db.Numeric(precision=8, asdecimal=False, decimal_return_scale=None))
like image 23
Alex Anzola Jürgenson Avatar answered Oct 19 '22 16:10

Alex Anzola Jürgenson