Getting the following error when I changed a table column from post_text to post_text1. I've updated my model and search accordingly
% (name, schema))
UnknownFieldError: No field named 'post_text1' in <Schema: ['id', 'post_text']>
And heres the file:
class Post(db.Model):
__searchable__ = ['post_text1', 'post_text2', 'post_text3']
id = db.Column(db.Integer, primary_key=True)
post_type = db.Column(db.String(256))
post_text1 = db.Column(db.String(256))
post_text2 = db.Column(db.String(256))
post_text3 = db.Column(db.String(256))
post_source = db.Column(db.String(256))
post_time = db.Column(db.Integer)
number_of_replies = db.Column(db.Integer)
post_score = db.Column(db.Integer)
post_rank = db.Column(db.Integer)
flagged = db.Column(db.Integer)
admin_post = db.Column(db.Boolean)
company_name = db.Column(db.String(64))
division = db.Column(db.String(64))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user_name = db.Column(db.String(256))
def __repr__(self):
return '<Post %r>' % (self.post_text1)
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
Suggestions? I tried uninstalling and reinstalling but it seems the uninstall still kept the old schema.
I got the same error and was able to fix it by deleting the whoosh_index directory that whoosh generates.
Related
I've two models Profile & Product representing One-Many relationship. One profile can have many products. I'm serializing all the fields. The column which has ForeignKey is coming out to be empty dictionary. The following model will make my issue more clear.
from backend_olx import db
from marshmallow import Schema, fields
from datetime import datetime
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
created_by = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=False)
purchased_by = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=True)
name = db.Column(db.String(50), nullable=False)
price = db.Column(db.Integer, nullable=False)
description = db.Column(db.Text, nullable=False)
def __repr__(self):
return '<Product Name %r>' % self.name
class Profile(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
email = db.Column(db.String(100), nullable=False, unique=True)
products_sold = db.relationship('Product', backref='profile_sold', foreign_keys="Product.created_by",lazy=True)
products_purchased = db.relationship('Product', backref='profile_purchased', foreign_keys="Product.purchased_by",lazy=True)
def __repr__(self):
return '<User %r>' % self.username
class ProfileSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str()
email = fields.Str()
products_sold = fields.Nested('ProductSchema', many=True)
products_purchased = fields.Nested('ProductSchema', many=True)
class ProductSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str()
price = fields.Int()
created_by = fields.Nested('ProfileSchema')
purchased_by = fields.Nested('ProfileSchema')
profile_schema = ProfileSchema()
profiles_schema = ProfileSchema(many=True)
product_schema = ProductSchema()
products_schema = ProductSchema(many=True)
The Nested() method in ProfileSchema is working as expected but it is giving { } in ProductSchema.
I want created_by and purchased_by fields to be populated as well.
How to go about ths?
You can't just pass a foreign key and expect Nested to know what to do about it. You need to pass a relation instead.
Create a relation for both fields and use the relation name in the schema.
I typically use xxx_id for column name and xxx for relation name.
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
created_by_id = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=False)
purchased_by_id = db.Column(db.Integer, db.ForeignKey('profile.id'), nullable=True)
name = db.Column(db.String(50), nullable=False)
price = db.Column(db.Integer, nullable=False)
description = db.Column(db.Text, nullable=False)
# Setup relations here
created_by = db.relationship(...)
purchased_by = db.relationship(...)
class ProductSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str()
price = fields.Int()
created_by = fields.Nested('ProfileSchema')
purchased_by = fields.Nested('ProfileSchema')
I have an sqlite database in my flask server with three tables as seen below:
from database import db
class ChordNode(db.Model):
__tablename__ = 'chordnode'
id = db.Column(db.Integer, primary_key=True)
hashed_id = db.Column(db.String)
successor = db.Column(db.String)
predecessor = db.Column(db.String)
is_bootstrap = db.Column(db.Boolean, default=False)
storage = db.relationship("KeyValuePair")
node_map = db.relationship("NodeRecord", cascade="delete")
def __repr__(self):
return 'Chord node {}, with successor: {}, predecessor: {}, ' \
'bootstrap: {}'.format(self.hashed_id, self.successor,
self.predecessor, self.is_bootstrap)
class KeyValuePair(db.Model):
__tablename__ = 'keyvaluepair'
id = db.Column(db.Integer, primary_key=True)
chordnode_id = db.Column(db.String, db.ForeignKey('chordnode.hashed_id'), nullable=True)
hashed_id = db.Column(db.String)
value = db.Column(db.String)
def __repr__(self):
return '<key-value pair: {}:{}, responsible Chord node: {}'.format(self.hashed_id,
self.value, self.chordnode_id)
class NodeRecord(db.Model):
__tablename__ = 'noderecord'
id = db.Column(db.Integer, primary_key=True)
bootstrap_id = db.Column(db.Integer, db.ForeignKey('chordnode.id'), nullable=False)
ip_port = db.Column(db.String)
def __repr__(self):
return 'Node record {} on boo
tstrap node with id {}'.format(self.ip_port,
self.bootstrap_id)
When i insert into ChordNode class or Noderecord everything works fine but inserting a record into KeyValuePair produces this error:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) FOREIGN KEY constraint failed
However KeyValuePair's foreign key is exactly the same as ChordNode's corresponding.
Any ideas about what else could be wrong with this and why this error occurs?
from datetime import datetime
from pack import db,login_manager
from flask_login import UserMixin
# SQLALCHEMY_TRACK_MODIFICATIONS = False
#login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique = True, nullable = False)
email = db.Column(db.String(150), unique = True, nullable = False)
password = db.Column(db.String(60), nullable = False)
profile_pic = db.Column(db.String(20), nullable = False, default = "default_profile_pic.jpg")
posts = db.relationship('Post', backref = 'author', lazy = True)
comments = db.relationship('Comment', backref = 'commentor', lazy = True)
def __repr__(self):
return f"User('{self.username}','{self.email}','{self.profile_pic}')"
class Post(db.Model,UserMixin):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable = False)
date_posted = db.Column(db.DateTime, nullable = False, default = datetime.utcnow)
content = db.Column(db.Text, nullable = False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)
post_comments = db.relationship('Comment', backref = 'commentor', lazy = 'dynamic')
def __repr__(self):
return f"Post('{self.title}','{self.content}','{self.date_posted}','{self.comments}')"
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
comment = db.Column(db.String(100000), nullable = False)
timestamp = db.Column(db.DateTime, nullable = False, default = datetime.utcnow, index = True)
user_comment_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)
post_comment_id = db.Column(db.Integer, db.ForeignKey('post.id'))
def __repr__(self):
return f"Post('{self.comment}','{self.user_comment_id}','{self.post_comment_id}')"
This give this error:
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Post->post'. Original exception was: Error creating backref 'commentor' on relationship 'Post.post_comments': property of that name exists on mapper 'mapped class Comment->comment'
Please help me resolve this error. I want to add a feature to comment on a post and for that i am trying to connect the Comment model with Post and User models.
In your error you have: Post.post_comments': property of that name exists on mapper 'mapped class Comment->comment.
It seems that in User you have comments which creates a backref 'commentor' in Comment and in Post you have post_comments which also creates a backref 'commentor' in Comment.
For example in Post, you can change post_comment to :
post_comments = db.relationship('Comment', backref = 'post_commented', lazy = 'dynamic')
Edit:
When defining a One to Many relationship with backref parameter, there is no need of user_id (in Post), user_comment_id (in Comment), post_comment_id (in Comment): Simply remove them.
You should have another look at https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#one-to-many (the backref part)
I have one to many relationships between tables in a database and I want to get the data using an API.
I think that ma.Nested does not work because I don't get all the fields
can anyone help me?
I get only this :
[
{
"IsRef": false,
"commands": [
"299d7f0b-721c-484b-9448-072716a5fd70",
"382c5d9f-99a1-4aa2-ac30-96084e202fad",
"299d7f0b-721c-484b-9448-072716a5fd75",
"382c5d9f-99a1-4aa2-ac30-96084e202fak"
],
"filename": "uiyg",
"version": 8
}
]
this is the database model :
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255), unique=True)
version = db.Column(db.Integer, unique=True)
IsRef = db.Column(db.Boolean)
commands = db.relationship('Command', backref='log', lazy='joined')
def __init__(self, filename, version, IsRef):
self.filename = filename
self.version = version
self.IsRef = IsRef
class Command(db.Model):
id_command = db.Column(db.String(255), primary_key=True, autoincrement=False)
name = db.Column(db.String(255))
log_id = db.Column(db.Integer, db.ForeignKey('file.id'))
status = db.Column(db.Boolean)
events = db.relationship('Event', backref='com', lazy='joined')
def __init__(self, id_command, name, log_id, status):
self.id_command = id_command
self.name = name
self.log = log_id
self.status = status
class Event(db.Model):
id_event = db.Column(db.Integer, primary_key=True)
event_name = db.Column(db.String(255))
seq_number = db.Column(db.Integer)
com_id = db.Column(db.Integer, db.ForeignKey('command.id_command'))
def __init__(self, event_name, seq_number, com_id):
self.event_name = event_name
self.seq_number = seq_number
self.com = com_id
this is the schema :
class EventSchema(ma.ModelSchema):
class Meta:
model = Event
fields = ('event_name', 'seq_number')
class CommandSchema(ma.ModelSchema):
class Meta:
model = Command
events = ma.Nested(EventSchema)
fields = ('id_command', 'name', 'status', 'events')
class LogSchema(ma.ModelSchema):
class Meta:
model = File
commands = ma.Nested(CommandSchema)
fields = ('filename', 'version', 'IsRef', 'commands')
Log_schema = LogSchema(many=True)
this is the API :
#app.route("/getall/<version>", methods=["GET"])
def get_all(version):
alle = File.query.filter_by(version=version)
return Log_schema.jsonify(alle)
this question is solved. I've changed 'lazy ='joined'' to 'lazy='dynamic'' and I've put ma.Nested out of the Meta class
I've read through all of these (https://stackoverflow.com/search?q=sqlalchemy.exc.NoReferencedTableError%3A), the Flask-appbuilder docs, the sqlalchemy docs, and the Flask-sqlalchemy docs and more. Unfortunately, I can't find any full examples of a many-to-many sqlalchemy relationship.
I have a python Flask app using flask-appbuilder (which relies on flask-sqlalchemy). My app/model.py file has this:
field_feature_association = Table('field_feature_association',Base.metadata,
Column('field_id', Integer, ForeignKey('field.id')),
Column('feature_id',Integer, ForeignKey('feature.id')),
schema="main"
)
class field(Model):
__tablename__ = 'field'
id = Column(Integer, primary_key=True)
name = Column(String(70), nullable=False)
database_type = Column(String(70)) #varchar(255), text, int
joinable_to = Column(Text())
notes = Column(Text()) #don't use this for X
table_id = Column(Integer, ForeignKey('table.id'))
table = relationship("table")
features = relationship("feature",
secondary = field_feature_association,
backref = backref('fields'),
)
def __repr__(self):
return self.name
class feature(Model):
__tablename__ = 'feature'
id = Column(Integer, primary_key=True)
name = Column(String(70), unique = True, nullable=False)
field_id = Column(Integer, ForeignKey('field.id'))
#field = relationship("field")
def __repr__(self):
return self.name
It's generating this error:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'field_feature_association.feature_id' could not find table 'feature' with which to generate a foreign key to target column 'id'
Thoughts on how to fix this error?
Here is a working sample of many-to-many in SQLAlchemy. Moreover I modified your model and it works fine:
field_feature_association = db.Table('field_feature_association', db.Model.metadata,
db.Column('field_id', db.Integer, db.ForeignKey('field.id')),
db.Column('feature_id', db.Integer, db.ForeignKey('feature.id')),
schema="main"
)
class Field(db.Model):
__tablename__ = 'field'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(70), nullable=False)
database_type = db.Column(db.String(70)) # varchar(255), text, int
joinable_to = db.Column(db.Text())
notes = db.Column(db.Text()) # don't use this for X
features = db.relationship("Feature",
secondary=field_feature_association,
backref=db.backref('fields'),
)
def __repr__(self):
return self.name
class Feature(db.Model):
__tablename__ = 'feature'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(70), unique=True, nullable=False)
field_id = db.Column(db.Integer, db.ForeignKey('field.id'))
# field = relationship("field")
def __repr__(self):
return self.name
and this is how to use it:
field = Field()
field.name="filed1"
feature = Feature()
feature.name = "feature1"
field.features.append(feature)
db.session.add(field)
db.session.commit()
My database object is imported as ’db’ and I have used it explicitely to refer to other types.