will
will

Reputation: 679

How to Store a List Within a Model in Flask SQLAlchemy

I am trying to store a list within a model using Flask's SQLAlchemy library. The data stored would be a list of latitude longitude points like so:

['41.0282', '73.7787']

I would like to store a value like this inside my User table like the one below:

class User(UserMixin, db.Model):
__tablename__ = 'users'
__table_args__ = {'extend_existing': True}

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
email = db.Column(db.String(64), unique=True, index=True)

Anyone know how I can go about this?

Upvotes: 0

Views: 3481

Answers (1)

b0lle
b0lle

Reputation: 942

There is no way to store a list in SQL. The proper way to handle multiple values is to create a new table and reference to that table with foreign keys.

If thats to heavyweight you can also store the values as a string and seperate them after your sql query.

This is then stored in your DB "41.0282, 73.7787" To get your original list (python):

"41.0282, 73.7787".split(',')

Upvotes: 1

Related Questions