How can I include grandchildren with flask-restless? - python

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)

Related

sqlalchemy populate value based on relationship

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 ?

how to use primary and foreign keys in SQL ALchemy

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))

sqlalchemy.exc.NoReferencedTableError: Foreign key could not find table

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")

sqlalchemt---SAWarning: fully NULL primary key identity cannot load any object

model:
'''任务工单'''
__tablename__ = 'task'
id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
user_id = db.Column(db.Integer, index=True, nullable=False)
dep_id = db.Column(db.Integer, index=True, nullable=False)
title = db.Column(db.String(200), nullable=False, index=True)
content = db.Column(db.Text, nullable=False)
cate = db.Column(db.SmallInteger, default=1,`enter code here`)
add_time = db.Column(db.Integer, default=int(time.time()), comment="添加时间")
is_top = db.Column(db.SmallInteger, default=0, comment="是否加急 1--加急 0--不加急")
desgin_id = db.Column(db.Integer,comment="设计人员的id")
update_time = db.Column(db.Integer, comment="修改时间")
review_time = db.Column(db.Integer, comment="审核时间")
assignment_time = db.Column(db.Integer, comment='分配时间')
fiannce_time = db.Column(db.Integer, comment='完成时间')
invalid_time = db.Column(db.Integer, comment='无效时间')
desgin_time = db.Column(db.Integer, comment="开始设计时间")
state = db.Column(db.SmallInteger, default=1, comment="状态:1--初始化待分配 2--带设计 3--设计中 4--提交待审核(审核中) 5--驳回 7--完成 0--无效")
use:
taskDicts = Task.query.filter_by(state=state).order_by(desc(Task.is_top),asc(Task.add_time)).offset(offset).limit(10).all()
venv/lib/python3.7/site-packages/sqlalchemy/orm/loading.py:246:
SAWarning: fully NULL primary key identity cannot load any object.
This condition may raise an error in a future release.

Define primary and foreign key to same field in Flask SqlAclhemy

I have the following definitions
class Usuario(db.Model):
__tablename__ = 'usuarios'
id_usuario = db.Column(db.Integer, primary_key = True)
id_centro = db.Column(db.Integer)
creado_en = db.Column(db.DateTime)
actualizado_en = db.Column(db.DateTime)
gcm_id = db.Column(db.Text)
notificaciones = db.relationship('RelNotificacionUsuario', backref='usuario', lazy='dynamic')
def __init__(self, id_usuario, id_centro):
self.id_usuario = id_usuario.title()
self.id_centro = id_centro.title()
self.gcm_id = gcm_id.title()
class Notificaciones(db.Model):
__tablename__ = 'notificaciones'
id_notificacion = db.Column(db.Integer, primary_key = True)
descripcion = db.Column(db.Text)
creado_en = db.Column(db.DateTime)
actualizado_en = db.Column(db.DateTime)
fecha = db.Column(db.DateTime)
notificaciones = db.relationship('RelNotificacionUsuario', backref='notificacion', lazy='dynamic')
def __init__(self, id_usuario, id_centro):
self.id_usuario = id_usuario.title()
self.id_centro = id_centro.title()
self.gcm_id = gcm_id.title()
class RelNotificacionUsuario(db.Model):
__tablename__ = 'rel_notificacion_usuario'
id_usuario = db.Column(db.Integer, primary_key = True, db.ForeignKey('usuarios.id_usuario'))
id_centro = db.Column(db.Integer, primary_key = True, db.ForeignKey('notificaciones.id_notificacion'))
creado_en = db.Column(db.DateTime)
actualizado_en = db.Column(db.DateTime)
gcm_id = db.Column(db.Text)
Well the following lines are failing SyntaxError: non-keyword arg after keyword arg
id_usuario = db.Column(db.Integer, primary_key = True, db.ForeignKey('usuarios.id_usuario'))
id_centro = db.Column(db.Integer, primary_key = True, db.ForeignKey('notificaciones.id_notificacion'))
According to docs http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html#sqlalchemy.schema.Column
This is the good way:
id_usuario = db.Column(db.Integer, db.ForeignKey('usuarios.id_usuario'), primary_key = True)
id_centro = db.Column(db.Integer, db.ForeignKey('notificaciones.id_notificacion'), primary_key = True)

Categories