I'm trying to reuse database table classes /models
my model is as below restmodels.py
# attributes master - holds the main product attributes
class AttributesMst(db.Model):
__tablename__ = 'oc_attribute'
attribute_id = db.Column(db.INT, primary_key=True, autoincrement=True, nullable=False)
attribute_group_id = AttributesGroups.attribute_group_id
sort_order = db.Column(db.INT, nullable=False)
#Attribute Groups
class AttributesGroups(db.Model):
__tablename__ = 'oc_attribute_group'
attribute_group_id = db.Column(db.INT, primary_key=True, autoincrement=True, nullable=False)
sort_order = db.Column(db.INT, nullable=False)
# attribute descriptions and language
class AttributesDescrptn(db.Model):
__tablename__ = 'oc_attribute_description'
attribute_id = AttributesMst.attribute_id
language_id = db.Column(db.INT, primary_key=True, autoincrement=False, nullable=False)
name = db.Column(db.VARCHAR(64), nullable=False)
When I run the code I get the following Traceback.
Traceback (most recent call last):
File "/home/seroney/PycharmProjects/TestProject/Addresses/restmodels.py", line 108, in <module>
class AttributesMst(db.Model):
File "/home/seroney/PycharmProjects/TestProject/Addresses/restmodels.py", line 111, in AttributesMst
attribute_group_id = AttributesGroups.attribute_group_id
NameError: name 'AttributesGroups' is not defined
Please note the classes are in the same file. Regards.
What I'm trying to achieve is reuse a table column like on Play Framework.
You define AttributesGroups
after you reference it. That is why you get the NameError
. In the end, however, I think you're trying to create a foreign key. (Having new used Play Framework, though, I could be wrong.)
To create a foreign key with SQLAlchemy, replace
attribute_group_id = AttributesGroups.attribute_group_id
with
attribute_group_id = db.Column(db.Integer, db.ForeignKey('oc_attribute_groups.attribute_group_id'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.