I have two tables:
class Ticket(db.Model):
ticketID = db.Column(db.Integer, primary_key=True)
uidCreator = db.Column(db.Integer, db.ForeignKey("User.id"))
uidEmployee = db.Column(db.Integer, db.ForeignKey("User.id"))
ticketType = db.Column(db.String(50))
ticketComments = db.Column(db.String(500))
empTicketComments = db.Column(db.String(500))
empDateRespond = db.Column(db.DateTime(timezone=True))
empDateTaken = db.Column(db.DateTime(timezone=True))
dateCreated = db.Column(db.DateTime(timezone=True), default=func.now())
dateResolved = db.Column(db.DateTime(timezone=True))
Status = db.Column(db.String(24))
department =
And
class DepartmentTickets(db.Model):
id = db.Column(db.Integer(), primary_key=True)
department = db.Column(db.String(100))
ticketType = db.Column(db.String(100), unique=True)
How can I return the department for my Ticket.department column where Ticket.ticketType == DepartmentTickets.ticketType ?
Related
I am trying to get the price value from my EVENTS class as an INT so that I can use it to make a booking by multiplying by the number of attendees for an event when I insert a new row in the booking table.
Tables in databse
class Event(db.Model):
__tablename__ = 'events'
id = db.Column(db.Integer, primary_key=True)
host = db.Column(db.String(80), nullable=False)
event_title = db.Column(db.String(80), nullable=False)
event_description = db.Column(db.String(80), nullable=False)
movie_name = db.Column(db.String(80), nullable=False)
movie_description = db.Column(db.String(80), nullable=False)
genre = db.Column(db.String(80), nullable=False)
movie_start_time = db.Column(db.Time(), nullable=False)
movie_end_time = db.Column(db.Time(), nullable=False)
classification = db.Column(db.String(80), nullable=False)
rating = db.Column(db.Integer(), nullable=False)
actors = db.Column(db.String(200), nullable=False)
directors = db.Column(db.String(200), nullable=False)
event_date = db.Column(db.Date(), nullable=False)
published_date = db.Column(db.Date(), nullable=False)
published_by = db.Column(db.String(80), nullable=False)
image = db.Column(db.String(60), nullable=False)
capacity = db.Column(db.Integer(), nullable=False)
address = db.Column(db.String(80), nullable=False)
status = db.Column(db.String(80), nullable=False)
price = db.Column(db.Integer(), nullable=False)
# ... Create the Comments db.relationship
# relation to call destination.comments and comment.destination
comments = db.relationship('Comment', backref='event')
class Booking(db.Model):
__tablename__ = 'bookings'
id = db.Column(db.Integer, primary_key=True, unique=True)
attendees = db.Column(db.Integer(), nullable=False)
total_price = db.Column(db.Integer(), nullable=False)
booked_at = db.Column(db.DateTime, default=datetime.now())
# foreign key
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
event_id = db.Column(db.Integer, db.ForeignKey('events.id'))
Code calling from Events table to get price and insert new row into Booking table
price = Event.query.with_entities(Event.price).filter_by(id = event)
user_name = User.query.with_entities(User.id).filter_by(name = current_user.name)
num_attendees = forms.attendees.data
print(num_attendees)
print(type(price))
booking = Booking(attendees=forms.attendees.data,
total_price= price * num_attendees,
user_id = user_name,
event_id = event)
#here the back-referencing works - comment.destination is set
# and the link is created
db.session.add(booking)
db.session.commit()
But I keep running into errors such as TypeError: unsupported operand type(s) for *: 'Row' and 'int'
Thank you
I am creating a site where you can log in and then add your favorite animes and movies to your favorite list, for that I created a database and 3 tables User, Anime and Movie.
My database code:
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
first_name = db.Column(db.String(150))
anime = db.relationship('Anime')
movie = db.relationship('Movie')
class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
date = db.Column(db.DateTime(timezone=True), default=func.now())
year = db.Column(db.Integer, nullable=False)
description = db.Column(db.String(400), nullable=False)
rating = db.Column(db.Float, nullable=True)
ranking = db.Column(db.Integer, nullable=True)
review = db.Column(db.String(50), nullable=True)
img_url = db.Column(db.String(250), nullable=False)
movie_link = db.Column(db.String(250), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class Anime(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
year = db.Column(db.Integer, nullable=False)
date = db.Column(db.DateTime(timezone=True), default=func.now())
description = db.Column(db.String(400), nullable=False)
rating = db.Column(db.Float, nullable=True)
ranking = db.Column(db.Integer, nullable=True)
review = db.Column(db.String(50), nullable=True)
img_url = db.Column(db.String(250), nullable=False)
anime_link = db.Column(db.String(250), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
I want to first filter all the data with the current user id
(all_anime = Anime.query.filter_by(id=current_user.id.).all())
then sort them according to the ratings given by the user
(all_anime = Anime.query.order_by(Anime.rating).all())
all_anime = User.anime.query.order_by(Anime.rating).all()
all_anime = db.session.query(Anime).filter_by(id=current_user.id).all()
print(all_anime)
for i in range(len(all_anime)):
all_anime[i].ranking = len(all_anime) - i
db.session.commit()
I know this snippet of code is wrong but I want to perform two different functions in two different columns how can I do it.
I also wanted to not accept the anime titles which are already present in the list of that user.
for that purpose, I have to filter the data with that user id
(anime_name = Anime.query.filter_by(id=current_user.id).all())
and then filter the data with the anime title
(anime_name = Anime.query.filter_by(title=data['canonicalTitle']).first()))
#views.route("/anime/find")
def find_anime():
anime_api_id = request.args.get("id")
if anime_api_id:
movie_api_url = f"https://kitsu.io/api/edge/anime//{anime_api_id}"
response = requests.get(movie_api_url)
data = response.json()['data']['attributes']
name = data["canonicalTitle"].replace(' ', '%20')
description = data['description'].rsplit('.', 1)[0]
# anime_name = User.anime.query.filter_by(title=data['canonicalTitle']).first()
# if anime_name:
# flash("Anime Already exists In The List.", category="error")
# else:
new_anime = Anime(
title=data['canonicalTitle'],
year=data['startDate'].split("-")[0],
img_url=data['posterImage']['medium'],
description=description,
anime_link=ANIME_SEARCH + name,
user_id=current_user.id
)
db.session.add(new_anime)
db.session.commit()
flash("Successfully Added.", category="success")
return redirect(url_for("views.edit_anime", id=new_anime.id))
I have the following two tables. User and Project.
I need User_ID and name to be projected in Project table columns user_ID and User respectively but i couldn't achieve it.
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
User_ID = db.Column(db.Integer, unique=True)
name = db.Column(db.String(20))
project = db.relationship('Project', backref='proj')
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
user = db.Column(db.String(50), db.ForeignKey("user.name"))
UserID = db.Column(db.Integer, db.ForeignKey("user.User_ID"))
customer = db.Column(db.String(50))
lead_office = db.Column(db.String(50))
phase = db.Column(db.String(50))
Try this ( it worked for me in similar project):
class User(db.Model):
User_ID = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
id_user = db.Column(db.String(50), db.ForeignKey("User.User_ID"))
customer = db.Column(db.String(50))
lead_office = db.Column(db.String(50))
phase = db.Column(db.String(50))
I have some models that i'd like to migrate, 2 of them are:
FamilyMember.py
class FamilyMember(db.Model):
__tablename__ = 'family_members'
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('users.id'))
name = db.Column(db.String(120), index=True)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(128), nullable=False)
notification = db.Column(db.Boolean, default=True)
auto_ml = db.Column(db.Boolean, default=True)
photo = db.Column(db.String(128), nullable=True)
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
notifications = db.relationship('Notification', backref='family_members', lazy='dynamic')
And Notification.py
class Notification(db.Model):
__tablename__ = 'notifications'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
fm_id = db.Column(db.Integer, db.ForeignKey('family_members.id'))
text = db.Column(db.String(255))
read = db.Column(db.Boolean, default=False)
type = db.Column(db.Integer)
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
Regarding to this post, i have to explicitly state table name with __tablename__ = 'tablename', i've done that but it didn't work the way it supposed to and still got the error sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'notifications.fm_id' could not find table 'family_members' with which to generate a foreign key to target column 'id'. What should i do?
You can use back_populates instead of backref in db.relationship()
class Notification(db.Model):
__tablename__ = 'notifications'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
fm_id = db.Column(db.Integer, db.ForeignKey('family_members.id'))
text = db.Column(db.String(255))
read = db.Column(db.Boolean, default=False)
type = db.Column(db.Integer)
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
family_members = db.relationship("FamilyMember", back_populates="notifications")
class FamilyMember(db.Model):
__tablename__ = 'family_members'
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('users.id'))
name = db.Column(db.String(120), index=True)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(128), nullable=False)
notification = db.Column(db.Boolean, default=True)
auto_ml = db.Column(db.Boolean, default=True)
photo = db.Column(db.String(128), nullable=True)
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
notifications = db.relationship("Notification", back_populates="family_member")
I have the following entities. When I do api/event/1, I get the invitation included, but not the invitationResponses. If, and how, is this possible to achieve?
When I do api/invitation/1 the invitationResponses are included.
class Person(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(64), index = True)
owner = db.Column(db.Integer, db.ForeignKey('user.id'))
description = db.Column(db.String(128))
start = db.Column(db.DateTime)
end = db.Column(db.DateTime)
invitation = db.relationship('Invitation', uselist=False, backref = db.backref('event', uselist=False))
class Invitation(db.Model):
id = db.Column(db.Integer, primary_key = True)
event_id = db.Column(db.Integer, db.ForeignKey('event.id'))
sent = db.Column(db.DateTime)
expires = db.Column(db.DateTime)
invitationResponses = db.relationship('InvitationResponse', backref = db.backref('invitation', uselist=False))
class InvitationResponse(db.Model):
id = db.Column(db.Integer, primary_key = True)
invitation_id = db.Column(db.Integer, db.ForeignKey('invitation.id'))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
attending = db.Column(db.Boolean)