delete one-to-one relationship in flask - python

I'm currently developing an application using flask and I'm having a big problem to delete items in an one-to-one relationship. I have the following structure in my models:
class User(db.Model):
__tablename__ = 'user'
user_id = db.Column(db.String(8), primary_key = True)
password = db.Column(db.String(26))
class Student(db.Model):
__tablename__ = 'student'
user_id = Column(db.String(8), ForeignKey('user.user_id'), primary_key = True)
user = db.relationship('User')
class Professor(db.Model):
__tablename__ = 'professor'
user_id = Column(db.String(8), ForeignKey('user.user_id'), primary_key = True)
user = db.relationship('User')
What I want to do is delete the Student or the Professor if I delete the user. I have tried to test it using the code below, however, when I check my database, the student and professor are still there and my tests don't pass. I tried to include the cascade parameter when I set the relationship but it doesn't work. I have found in the internet to use this parameter: single_parent=True, but it also doesn't work.
user1 = User(user_id='user1234',password='alsdjwe1')
user2 = User(user_id='user2345',password='asfr5421')
student1 = Student(user = user1)
professor1 = Professor(user = user2)
db.session.delete(user1)
db.session.delete(user2)
I'd be glad if somebody can help me with this.
Thank you very much,
Thiago.

Use the cascade argument in your relationships.
class Student(db.Model):
__tablename__ = 'student'
user_id = Column(db.String(8), ForeignKey('user.user_id'), primary_key = True)
user = db.relationship('User', cascade='delete')
class Professor(db.Model):
__tablename__ = 'professor'
user_id = Column(db.String(8), ForeignKey('user.user_id'), primary_key = True)
user = db.relationship('User', cascade='delete')
You might want to look into delete-orphan if your use case needs it.

Related

Python SQLalchemy Join lookup by User

Wrapping my head around a way to get a list of Jobs associated to a User. My DB Model goes a little something like this.
class Job(db.Model):
id = db.Column(db.Integer, primary_key=True)
# Relationship Rows
actions = db.relationship('JobAction', backref='job')
class JobAction(db.Model):
id = db.Column(db.Integer, primary_key=True)
# Linked Rows
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# Relationship Rows
user = db.relationship('User', foreign_keys=[user_id], backref='jobactions')
I need to get a list of Jobs that are associated to a User. I can use either the User already matching a logged in users details. Or the user.id.
I was looking at something like the below, but no dice. I can see it's overly optimistic a query, but can't see what's up. Potentially a missing Join.
# Get User first.
user = User.query.filter_by(id=1).first()
# Get their Jobs
jobs = Job.query.filter_by(actions.user=user).all()
Any ideas would be greatly appreciated.
Cheers,
I'm guessing you are missing a foreign key. If your database model looked like this:
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
jobactions = db.relationship("JobAction", back_populates="user")
class Job(db.Model):
__tablename__ = 'jobs'
id = db.Column(db.Integer, primary_key=True)
jobactions = db.relationship('JobAction', backref='job')
class JobAction(db.Model):
__tablename__ = 'jobactions'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'))
user = db.relationship(User, back_populates="jobactions")
job = db.relationship(Job, back_populates="jobactions")
Then you could use:
jobs = [ jobaction.job for jobaction in user.jobactions ]

Flask-SQLAlchemy query in a many to many ITSELF relationship

I have a many to many relationship to itself, that means, my model is User and one field is friends, which would be Users. I have done it the relationship but my problem comes when I try to do the query for the friends of an User. My model and the relation looks like this:
friendship = db.Table('friends',
Column('friend_id', db.Integer, db.ForeignKey('monkeys.id')),
Column('myfriend_id', db.Integer, db.ForeignKey('monkeys.id'))
)
class Monkey (db.Model):
_tablename__ = "monkeys"
id = Column(Integer, primary_key=True)
username = Column(String(50), unique = True)
email = Column(String(120), unique = True)
password = Column(String(50))
date = Column (DateTime(), default=datetime.now())
friends = relationship('Monkey',
secondary = friendship,
primaryjoin = (friendship.c.friend_id == id),
secondaryjoin = (friendship.c.myfriend_id == id),
backref = backref('friendship', lazy = 'dynamic'),
lazy = 'dynamic')
And from de view, if I want to do the query, I have tried with something
friends_list = Monkey.query.join(Monkey.friends).filter(Monkey.id == user.id).all()
Bu it does not work... any help please? thanks!
You don't need to create join by yourself.
change lazy='joined' - items will be loaded “eagerly” in the same query as that of the parent.
When You get object Monkey you already have access to friends
monkey = session.query(Monkey).get(user.id)
friends_list = monkey.friends

Filed in SQLAlchemy of user's list

I have one model that is Users in which there is a field in this model that I would like to store a list of Users. The idea is that you can add frieds and store them somewhere.
class User (db.Model):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(50), unique = True)
email = Column(String(120), unique = True)
password = Column(String(50))
date = Column(DateTime(), default=datetime.now())
friends = "Should be a list of users"
I have thought to have a string with the id of each user but, is there any posibility to do it with a relationship to the same model? like this:
friends = relationship("User")
Thanks a lot!
Proposed solutions based on Adjacency List Relationships would only work in case when someone can be a friend of maximum one person, which I do not believe to be the case in the real world.
A pattern you need to apply in this case is called Self-Referential Many-to-Many Relationship. Please read the sample linked to above. In order to make it work for your model, you would need to create additional table to keep the pairs of friends, and configure the relationship as below:
# object model
t_userfriend = Table("user_friend", Base.metadata,
Column("user_id", Integer, ForeignKey("users.id"), primary_key = True),
Column("friend_id", Integer, ForeignKey("users.id"), primary_key = True),
)
class User (Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String(50), unique = True)
# ...
friends = relationship("User",
secondary = t_userfriend,
primaryjoin = (id == t_userfriend.c.user_id),
secondaryjoin = (id == t_userfriend.c.friend_id),
backref = "friend_of",
)
I guess that the other question you need to ask yourself is whether in your model if A is a friend of B, does this mean that B is a friend of A? In case this is true, you might want/need to:
either store just one side of the relationship, and calculate the other
make sure you always store both sides to the relationship
You can use Adjacency List Relationship and this link have the same issue so you can learn from it.
How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself
Yes you can do it with Adjacency List Relationships.
class User (db.Model):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(50), unique = True)
email = Column(String(120), unique = True)
password = Column(String(50))
date = Column(DateTime(), default=datetime.now())
friends = relationship("User",
backref=backref('parent', remote_side=[id])
)

Overriding the table name in Flask-Alchemy

I am creating a Flask application and accessing the MySQL database using Flask-Alchemy.
I have following Class to access a table:
class price_table(db.Model):
id = db.Column(db.Integer, primary_key = True)
trans_id = db.Column(db.Integer)
timestamp = db.Column(db.Integer)
order_type = db.Column(db.String(25))
price = db.Column(db.Numeric(15,8))
quantity = db.Column(db.Numeric(25,8))
def __repr__(self):
return 'id'
For the table 'price_table' this works brilliantly, but problem is I have a few tables with the same columns as 'price_table' from which I only know the name at runtime.
I want to reuse the class above so I thought I could change tablename to the name of the table I need to read, but that does not work, the program keeps reading the 'price-table'
How do I override the tablename at runtime?
You should use: __tablename__ :
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(50), unique=True)
email = Column(String(120), unique=True)
http://flask.pocoo.org/docs/0.12/patterns/sqlalchemy/
Based on the comment left by jbub I found the following solution that does the trick just as needed.
from app import db
def ClassFactory(name):
tabledict={'id':db.Column(db.Integer, primary_key = True),
'trans_id':db.Column(db.Integer),
'timestamp':db.Column(db.Integer),
'order_type':db.Column(db.String(25)),
'price':db.Column(db.Numeric(25,8)),
'quantity':db.Column(db.Numeric(25,8)),}
newclass = type(name, (db.Model,), tabledict)
return newclass
You can overwrite price_table.table.name attribute, yet keep in mind that it will affect your price_table model so, unless you want to use it to create a new specialized version of this table in the db and you are not interacting with price_table model - I wouldn't recommend that.

Sqlalchemy many to many mapping with extra fields

I created a many to many relationship with sqlalchemy like this:
subject_books = Table('subject_books', Base.metadata,
Column('subject_id', Integer, ForeignKey('subjects.id')),
Column('book_id', Integer, ForeignKey('books.id')),
Column('group', Integer)
)
class Subject(Base):
__tablename__ = 'subjects'
id = Column(Integer, primary_key=True)
value = Column(Unicode(255), unique=True)
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255))
isbn = Column(Unicode(24))
subjects = relationship('Subject', secondary=subject_books, collection_class=attribute_mapped_collection('group'), backref='books')
after that I created a test like following:
book = Book(title='first book',isbn='test')
book.subjects[0] = Subject(value='first subject')
book.subjects[1] = Subject(value='second subject')
session.add(book)
transaction.commit()
and it works fine. But what I really want is to store more than one subject with the same group value, so I tried the following test:
book = Book(title='first book',isbn='test')
book.subjects[0] = [Subject(value='first subject'),Subject(value='second subject')]
book.subjects[1] = [Subject(value='third subject'),Subject(value='forth subject')]
session.add(book)
transaction.commit()
but it does not work.
Can this be done using sqlalchemy?
Thanks in Advance
Razi
I think you are constructing wrong relation ship.
Your relation ship must be
book M2M subject
subject M2M group
So you have to create one more model for group and that must be assign as m2m in Subject
Your models will be like.
subject_books = Table('subject_books', Base.metadata,
Column('subject_id', Integer, ForeignKey('subjects.id')),
Column('book_id', Integer, ForeignKey('books.id')),
)
subject_group = Table('subject_groups', Base.metadata,
Column('group_id', Integer, ForeignKey('groups.id')),
Column('subject_id', Integer, ForeignKey('subjects.id')),
)
class Subject(Base):
__tablename__ = 'subjects'
id = Column(Integer, primary_key=True)
value = Column(Unicode(255), unique=True)
groups = relationship('Groups', secondary=subject_groups, backref='subjects')
class Groups(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
name = Column(Unicode(255), unique=True)
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255))
isbn = Column(Unicode(24))
subjects = relationship('Subject', secondary=subject_books, backref='books')
I also check the docs for attribute_mapped_collection. But each time I found that each key is associated with only one object not more then one. If you read anywhere then please provide the link so I can check that how it will be fit in your code.
I think this will be help you.

Categories