When I tried to add a new table to python/flask -
class UserRemap(db.Model):
name = db.Column(db.String(40))
email = db.Column(db.String(255))
password = db.Column(db.String(64))
flag = db.Column(db.String(1))
def __init__(self, name, email, password):
self.email = email
self.name = name
self.password = password
self.flag='N'
Here is table schema -
mysql> desc UserRemap;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| name | varchar(40) | NO | | NULL | |
| password | varchar(64) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| flag | char(1) | NO | | N | |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
when i go to interactive python shell and did from myapp import db ;
I get this error message,
from server import db; Traceback (most recent call last): File
"<stdin>", line 1, in <module> File "server.py", line 74, in
<module>
class UserRemap(db.Model):
File "/usr/lib/python2.7/site-packages/Flask_SQLAlchemy-0.15-py2.7.egg/flaskext/sqlalchemy.py",
line 467, in __init__
DeclarativeMeta.__init__(self, name, bases, d)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-linux-i686.egg/sqlalchemy/ext/declarative.py",
line 1336, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-linux-i686.egg/sqlalchemy/ext/declarative.py",
line 1261, in _as_declarative
"table-mapped class." % cls sqlalchemy.exc.InvalidRequestError: Class <class 'server.UserRemap'> does not have a __table__ or
__tablename__ specified and does not inherit from an existing table-mapped class.
Any thoughts on how to fix this
Per the Flask-SQLAlchemy docs inheriting from db.Model
will automatically setup the table name for you.
You are seeing this message because you don't have a primary key defined for that table. The error message is unhelpful, but adding a primary key will fix this problem.
You have to mention __tablename__
or __table__
to notify sqlalchemy for table in database.
class UserRemap(db.Model):
__tablename__ = 'UserRemap'
name = db.Column(db.String(40))
email = db.Column(db.String(255))
password = db.Column(db.String(64))
flag = db.Column(db.String(1))
def __init__(self, name, email, password):
self.email = email
self.name = name
self.password = password
self.flag='N'
Specify __tablename__
in your class definition.
class UserRemap(db.Model):
__tablename__ = 'UserRemap'
name = db.Column(db.String(40))
email = db.Column(db.String(255))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With