SQLalchemy-Flask: ArgumentError for one to many relationship - python

I am trying to use sqlalchemy to run queries for one to many relationship. I am having trouble getting my queries to run.
class Quote(db.Model):
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(1000))
category = db.Column(db.String(100))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
date_added = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
quote_cat = db.relationship("Quote", backref='category', lazy=True)
quote_id_ = db.Column(db.Integer, db.ForeignKey('quote.id'))
sqlalchemy.exc.ArgumentError: Mapper mapped class Category->category
could not assemble any primary key columns for mapped table 'category'

Your quote_cat backref references a property that already exists on the Quote class. Either remove this or change the backref value.
Here are the backref docs:
backref –
indicates the string name of a property to be placed on the related mapper’s class that will handle this relationship in the other direction

Related

Flask SQLAlchemy: Child table with multiple parents?

I'm new to flask_sqlalchemy, and while I understand how the one-to-many, and many-to-many relationships work, I'm struggling to understand how I can apply them to my specific data types. I have the following three tables: TeamStat, PlayerStat, and Stat which are loosely described as follows
class PlayerStat(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, nullable=False, default=datetime.datetime)
player_id = db.Column(db.Integer, db.ForeignKey('player.player_id'), nullable=False)
class TeamStat(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, nullable=False, default=datetime.datetime)
team_id = db.Column(db.Integer, db.ForeignKey('team.team_id'), nullable=False)
class Stat(db.Model):
id = db.Column(db.Integer, primary_key=True)
value = db.Column(db.String(80), nullable=False)
Since Stat is a generic table type, I would like to use it by both the PlayerStat table (for individual player stats), as well as the TeamStat table (as a sum of the stats for all players on a team). Can someone help me understand how I can refer one child table to multiple parent tables in this fashion?
Use association tables to bridge your stat table to your players and team tables. This example is pretty close to what you are already doing, except the date column is moved on to the stat record table and I've replaced your PlayerStat and TeamStat objects with unmapped tables.
I've assumed you have two ORM classes, Player and Team (not Flask-SQLAlchemy sorry but concept remains the same):
plr_stat_assc = Table('plr_stat_assc', Base.metadata,
Column('player_id', Integer, ForeignKey('player.id')),
Column('stat_id', Integer, ForeignKey('stat.id'))
)
team_stat_assc = Table('team_stat_assc', Base.metadata,
Column('team_id', Integer, ForeignKey('team.id')),
Column('stat_id', Integer, ForeignKey('stat.id'))
)
class Player(Base):
__tablename__ = 'player'
id = Column(Integer, primary_key=True)
stats = relationship("Stat", secondary=plr_stat_assc)
class Team(Base):
__tablename__ = 'team'
id = Column(Integer, primary_key=True)
stats = relationship("Stat", secondary=team_stat_assc)
class Stat(Base):
__tablename__ = 'stat'
id = Column(Integer, primary_key=True)
date = Column(DateTime, nullable=False, default=datetime.datetime)
value = Column(String(80), nullable=False)

How to fix sqlalchemy.exc.NoForeignKeysError in flask?

I have this error:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.car - there are no foreign keys linking these tables via secondary table 'user'. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.
127.0.0.1 - - [26/Jul/2019 23:05:40] "GET /user HTTP/1.1" 500 -
The following is my program
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
user_id = Column(Integer)
passport_number = Column(String(8), nullable=False, primary_key=True)
user_email = Column(String(10), nullable=False)
user_name = Column(String(10), nullable=False)
car = relationship('Car', secondary='user')
class Car(Base):
__tablename__ = 'car'
car_number = Column(Integer, primary_key=True, nullable=False)
car_model = Column(String(10), nullable=False)
user_passport_number = Column(String(8), ForeignKey('user.passport_number'), primary_key=True)
part = relationship('Part', secondary='car')
class Part(Base):
__tablename__ = 'part'
part_name = Column(String(10), primary_key=True, nullable=False)
part_price = Column(Integer, nullable=False)
car_number = Column(Integer, ForeignKey('car.car_number'), primary_key=True)
From the error message below:
...there are no foreign keys linking these tables via secondary table 'user'...
we understand that the error might be happening in the setting of the secondary table. Per the docs :
The default behavior of relationship() when constructing a join is that it equates the value of primary key columns on one side to that of foreign-key-referring columns on the other. We can change this criterion to be anything we’d like using the primaryjoin argument, as well as the secondaryjoin argument in the case when a “secondary” table is used.
Therefore, and assuming that you a have a one-to-many or one-to-one relationship between User and Car, you do not seem to need the secondary parameter:
class User(Base):
__tablename__ = 'user'
user_id = Column(Integer)
passport_number = Column(String(8), nullable=False, primary_key=True)
user_email = Column(String(10), nullable=False)
user_name = Column(String(10), nullable=False)
car = relationship('Car', backref='User') #removed secondary relationship
Also, note that you are passing a primary_key constraint to the foreign key field, which is used only in one-to-one relationships, in case it was not intentional. In case it was, then the docs show a simple example for the one-to-one tables:
class User(Base):
__tablename__ = 'user'
passport_number = Column(String(8), nullable=False, primary_key=True)
car = relationship("Car", uselist=False, back_populates="car")
class Car(Base):
__tablename__ = 'car'
id = Column(Integer, primary_key=True)
user_passport_number = Column(String(8), ForeignKey('user.passport_number'))
user = relationship("User", back_populates="car")

How to set one to many and one to one relationship at same time in Flask-SQLAlchemy?

I'm trying to create one-to-one and one-to-many relationship at the same time in Flask-SQLAlchemy. I want to achieve this:
"A group has many members and one administrator."
Here is what I did:
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
members = db.relationship('User', backref='group')
admin = db.relationship('User', backref='admin_group', uselist=False)
def __repr__(self):
return '<Group %r>' % (self.name)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
admin_group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
However I got an error:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join
condition between parent/child tables on relationship Group.members -
there are multiple foreign key paths linking the tables. Specify the
'foreign_keys' argument, providing a list of those columns which
should be counted as containing a foreign key reference to the parent
table.
Does anyone know how to do that properly?
The solution is to specify the foreign_keys argument on all relationships:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'))
admin_group_id = Column(Integer, ForeignKey('groups.id'))
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
members = relationship('User', backref='group', foreign_keys=[User.group_id])
admin = relationship('User', backref='admin_group', uselist=False, foreign_keys=[User.admin_group_id])
Perhaps consider the admin relation in the other direction to implement "a group has many members and one admin":
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'))
group = relationship('Group', foreign_keys=[group_id], back_populates='members')
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
members = relationship('User', foreign_keys=[User.group_id], back_populates='group')
admin_user_id = Column(Integer, ForeignKey('users.id'))
admin = relationship('User', foreign_keys=[admin_user_id], post_update=True)
See note on post_update in the documentation. It is necessary when two models are mutually dependent, referencing each other.
The problem you're getting comes from the fact that you've defined two links between your classes - a User has a group_id (which is a Foreign Key), and a Group has an admin (which is also defined by a Foreign Key). If you remove the Foreign Key from the admin field the connection is no longer ambiguous and the relationship works. This is my solution to your problem (making the link one-to-one):
from app import db,app
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
admin_id = db.Column(db.Integer) #, db.ForeignKey('user.id'))
members = db.relationship('User', backref='group')
def admin(self):
return User.query.filter_by(id=self.admin_id).first()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
The one drawback to this is that the group object doesn't have a neat admin member object you can just use - you have to call the function group.admin() to retrieve the administrator. However, the group can have many members, but only one of them can be the administrator. Obviously there is no DB-level checking to ensure that the administrator is actually a member of the group, but you could add that check into a setter function - perhaps something like:
# setter method
def admin(self, user):
if user.group_id == self.id:
self.admin_id = user.id
# getter method
def admin(self):
return User.query.filter_by(id=self.admin_id).first()
Ok, I found a workaround for this problem finally. The many-to-many relationship can coexist with one-to-many relationship between the same two tables at the same time.
Here is the code:
groups_admins = db.Table('groups_admins',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('group_id', db.Integer, db.ForeignKey('group.id'))
)
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
members = db.relationship('User', backref='group')
admins = db.relationship('User',
secondary=groups_admins,
backref=db.backref('mod_groups', lazy='dynamic'),
lazy='dynamic')
def __repr__(self):
return '<Group %r>' % (self.name)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
I still want someone to tell me how to set one-to-many and one-to-one relationship at the same time, so I leave my answer here and won't accept it forever.
This link solved it for me
most important thing is to specify foreign_keys value in the relation as well as the primary join

SQLAlchemy different tables for relationship?

Suppose I have the following tables/relationships defined:
class Post(Base):
__table_name__ = "post"
id = Column(Integer, primary_key=True)
text = Column(String(100))
class Comment(Base):
__table_name__ = "comment"
id = Column(Integer, primary_key=True)
post_id = Column(Integer, ForeignKey("post.id"), nullable=False)
text = Column(String(100))
Now, I want to have notifications of events, like "You were tagged in a comment" or "you were tagged in a post". Is there some way to have a foreign key relationship in SQLAlchemy that can point to either a comment or a post (or several other tables in reality)? Something like:
class Notification(Base):
__table_name__ = "notification"
id = Column(Integer, primary_key=True)
target = relationship(??either post or comment??)
user_id = Column(Integer, ForeignKey("users.id")
created_date = Column(Datetime, default=datetime.utcnow)
I suppose you could just put foreign keys to all the different types and make all but one null, but that seems ugly. I'd also rather not have multiple tables for each type of notification; as in comment_notification and post_notification. Any ideas?

Flask foreign_keys still shows AmbiguousForeignKeysError

I have two foreign keys in an entity refering to another entity.
Here is how it looks
class Review(db.Model):
__tablename__ = 'Review'
id = db.Column(db.Integer, primary_key = True)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
business_user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
user = db.relationship('User', foreign_keys=[user_id])
business_user = db.relationship('User', foreign_keys=[business_user_id])
and
class User(db.Model):
__tablename__ = 'User'
id = db.Column(db.Integer, primary_key = True)
reviews = db.relationship('Review', backref='user',
lazy='dynamic')
However, it still shows me an error saying
there are multiple foreign key paths linking the tables. Specify the
'foreign_keys' argument, providing a list of those columns which
should be counted as containing a foreign key reference to the parent
table
The above workaround is what I get from some other posts. I have checked and changed many times, and still no luck. I wonder if it's already correct or there is something I miss. Need help
Finally, I got the workaround after trying to figure out. In my case, I don't have to put backref in Review class. Instead, I should put the User backref in User class itself. So, it should look like below
class Review(db.Model):
__tablename__ = 'Review'
id = db.Column(db.Integer, primary_key = True)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
business_user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
user = relationship('User', backref='user_reviews', foreign_keys=user_id)
business_user = relationship("User", backref='business_user_reviews', foreign_keys=[business_user_id])
class User(db.Model):
__tablename__ = 'User'
id = db.Column(db.Integer, primary_key = True)
Here, both types of User have many Reviews. Then, when I need to get the list of reviews of both User, what I can do is
user = User.query.get(id)
user_reviews = User.user_reviews
business_user_reviews = user.business_user_reviews
And I am no longer running across this error.

Categories

Resources