Reputation: 672
I'm currently trying to create Models for my database in SQLAlchemy, but whenever I try to create my database I keep running into this error despite not even implementing an __init__
function.
TypeError:
__init__()
missing 1 required positional argument: 'item_type'
I'm sure I'm missing something stupid, but can someone point me in the right direction
Base Model:
class EntityBase(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
date_created = db.Column(db.TIMESTAMP, nullable=False, default=lambda: datetime.now())
Inherited Model:
class Recipe(db.Model):
__tablename__ = 'Recipes'
ingredients = db.Column(db.ARRAY, nullable=False)
steps = db.Column(db.ARRAY, nullable=False)
likes = db.Column(db.Integer, nullable=False, default=0)
views = db.Column(db.Integer, nullable=False, default=0)
title = db.Column(db.String, nullable=False)
description = db.Column(db.String)
StackTrace:
File "infrastructure\data\database_models\recipe.py", line 5, in <module>
class Recipe(db.Model):
File "infrastructure\data\database_models\recipe.py", line 8, in Recipe
ingredients = db.Column(db.ARRAY, nullable=False)
File "backend\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 1565, in __init__
super(Column, self).__init__(name, type_)
File "backend\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 4640, in __init__
self.type = type_api.to_instance(type_)
File "backend\venv\lib\site-packages\sqlalchemy\sql\type_api.py", line 1631, in to_instance
return typeobj(*arg, **kw)
TypeError: __init__() missing 1 required positional argument: 'item_type'
Upvotes: 0
Views: 639
Reputation: 780723
The db.ARRAY()
type requires the type of the array elements. So if ingredients
is an array of integers, it would be:
ingredients = db.Column(db.ARRAY(db.Integer()), nullable=False)
Upvotes: 3
Reputation: 361
It appears that the constructor from db.model has a required parameter. Because your Recipe class is derived from db.model it must have a constructor that calls the super class and supplies this value. This will also be true of anything derived from your EntityBase class. The following is an example of how to properly do this.
class Recipe(db.Model):
__tablename__ = 'Recipes'
def __init__(self, item_type):
super().__init__(item_type)
...
Upvotes: 0