How to add foreign key data in SQLALCHEMY_COMMIT_ON_TEARDOWN - sqlalchemy

in my model:
class User(db.Model):
__tablename__ = "user"
id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String(20))
class Product(db.Model):
__tablename__ = "product"
id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String(20))
userid = Column(ForeignKey("user.id"))
user = relationship("User",backref="product")
my flask run.py code:
...
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
...
#app.teardown_request
def dbsession_clean(excetion):
if app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] :
if excetion is None:
try:
db.session.commit()
except:
db.session.rollback()
db.session.remove()
...
my view code
...
t = User(id=9,name="y9")
p = Product(id=5,name="p5",user=User.query.get(9))
db.session.add(t)
db.session.flush()
db.session.add(p)
...
the result is that data has saved in table, but only userid field is NULL int product table
how i solute it? please and thk

Related

Access/Query List of Object SQLAlchemy

I have a relationship that yields a list of objects
class Category(db.Model):
_id = db.Column("id", db.Integer, primary_key = True)
book_category = db.Column("book_category", db.String)
booklist = db.relationship('Books', backref = "book_category")
def __init__(self, book_category):
self.book_category = book_category
class Books(db.Model):
_id = db.Column("id", db.Integer, primary_key = True)
bookname = db.Column("bookname", db.String)
filename = db.Column("filename", db.String)
category = db.Column(db.Integer, db.ForeignKey('category.book_category'))
def __init__(self, bookname,filename):
self.bookname = bookname
self.filename = filename
So when I'm querying the category I have an access to category.booklist
Which would yield something like this
[<book1>, <book3>, <book4>]
I can access each using for loop however, I'm feeling that it is not the most efficient way to do.
Is there any way that I can do like
category.booklist.query.filter(books.bookname == variable).all()
Querying and filtering the list of objects yielded by category.booklist

How to add a column with a count result to a sqlalchemy query and pass the pydantic check?

Description
Previously, my query returned the contents of a single Stories table. Now I want to add more information: I need to output the prizes_count for each Story. There is no field prizes_count in the Stories table so I made the following query.
db.query(models.Stories, func.count(models.Stories.prizes).label("prizes_count")).join(models.Prizes)\
.group_by(models.Stories.id).all()
But I have two problems with it.
I get validation errors from Pydantic, because this query returns a list of tuples like (<database.models.Stories object at 0x0000026BB0055E20>, 1). I have to insert the prizes_count value into the Stories object or vice versa, pull all fields into the tuple. I can do it manually, of course, but I think there is a better way.
With this query I lose all stories with 0 prizes because my join ignores them.
Code
endpoint
#app.get("/stories/", response_model=List[schemas.StoryFullInfo])
def get_stories(db: Session = Depends(get_db)):
return crud.get_stories(db)
crud
def get_stories(db: Session):
return db.query(models.Stories, func.count(models.Stories.prizes).label("prizes_count")).join(models.Prizes)\
.group_by(models.Stories.id).all()
models
class Stories(Base):
__tablename__ = "stories"
id = Column(INTEGER(unsigned=True), primary_key=True)
title = Column(String(length=128), index=True)
text = Column(String(length=1000))
author_id = Column(INTEGER(unsigned=True), ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False)
status = Column(TINYINT(unsigned=True), server_default="0")
genre_type = Column(TINYINT(unsigned=True), server_default="0")
likes_count = Column(INTEGER(unsigned=True), server_default="0")
image = Column(Text)
added_to_best_by = Column(INTEGER(unsigned=True))
creation_DT = Column(DateTime, server_default=func.now())
change_status_DT = Column(DateTime)
author = relationship("Users", back_populates="stories")
comments = relationship("Comments", back_populates="story")
prizes = relationship("Prizes", back_populates="story")
class Prizes(Base):
__tablename__ = "prizes"
id = Column(INTEGER(unsigned=True), primary_key=True)
title = Column(String(length=128), nullable=False)
image_id = Column(TINYINT(unsigned=True))
story_id = Column(INTEGER(unsigned=True), ForeignKey("stories.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False)
user_id = Column(INTEGER(unsigned=True), ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False)
text = Column(String(length=512), nullable=False)
creation_DT = Column(DateTime, server_default=func.now())
story = relationship("Stories", back_populates="prizes")
author = relationship("Users", back_populates="prizes")
schemas
class StoryBaseInfo(BaseModel):
id: int
title: str = None
author_id: int
class Config:
orm_mode = True
class StoryUpdateInfo(StoryBaseInfo):
#title: str = None
text: str = None
status: int
genre_type: int
likes_count: int
image: str = None
added_to_best_by: int = None
change_status_DT: datetime = None
class Config:
orm_mode = True
class StoryFullInfo(StoryUpdateInfo):
creation_DT: datetime
author: UserBaseInfo
prizes_count: int
class Config:
orm_mode = True
class PrizeBaseInfo(BaseModel):
id: int
story_id: int
class Config:
orm_mode = True
class PrizeInfo(PrizeBaseInfo):
title: str
image_id: int
text: str
creation_DT: datetime
author: UserBaseInfo
story: StoryBaseInfo
class Config:
orm_mode = True
Well, it turns out I was thinking in the wrong direction when I asked this. The problem is solved by the features of SQLAlchemy. I can count prizes by using my configured relationship.
My solution is to add the hybrid property to Stories SQLAlchemy model
class Stories(Base):
__tablename__ = "stories"
id = Column(INTEGER(unsigned=True), primary_key=True)
title = Column(String(length=128), index=True)
text = Column(String(length=1000))
author_id = Column(INTEGER(unsigned=True), ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False)
status = Column(TINYINT(unsigned=True), server_default="0")
genre_type = Column(TINYINT(unsigned=True), server_default="0")
likes_count = Column(INTEGER(unsigned=True), server_default="0")
image = Column(Text)
added_to_best_by = Column(INTEGER(unsigned=True))
creation_DT = Column(DateTime, server_default=func.now())
change_status_DT = Column(DateTime)
author = relationship("Users", back_populates="stories")
comments = relationship("Comments", back_populates="story")
prizes = relationship("Prizes", back_populates="story")
#hybrid_property
def prizes_count(self):
return len(self.prizes)
And then the following query will satisfy the Pydantic scheme.
def get_stories(db: Session):
return db.query(models.Stories).all()

Sqlalchemy - Insert into table if not exist in a column (Mysql)

I have a simple Table like this
class Employeecode(Base):
__tablename__ = 'employeecode'
id = Column(Integer, primary_key=True)
unique_code = Column(String(5), unique=True)
employe_name = Column(String(50))
designation = Column(String(50))
How i can create a function which will insert value into 'employeecode' table if 'unique_code' column doesn't have that value.
insert_unique(unique_code,employe_name,designation)
First you can store all previous unicode_value in a list and then you can check and run your query..
def function(**kwargs):
session = Session()
unique_list =[]
employeecodes = db.session.query(Employeecode).all()
for employee in employeecodes:
unique_list.append(employee.unique_code)
if kwargs['unique_code'] not in unique_list:
employee = Employeecode()
employee.unique_code = kwargs['unique_code']
employee.employe_name = kwargs['employe_name']
employee.designation = kwargs['designation']
session.commit()
retval = row2dict(employee)
session.close()
return retval
else:
pass

Accessing extra column in many-to-many relationship with simplified association objects

I'm trying to work with the example in the SQLAlchemy docs: Simplifying Association Objects
What I am struggling with understanding is how I can access the special_key. Ultimately I'd like to be able to do something like this:
for user in users
for keyword in user.keywords
keyword.special_key
Here is the code from the example:
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(64))
# association proxy of "user_keywords" collection
# to "keyword" attribute
keywords = association_proxy('user_keywords', 'keyword')
def __init__(self, name):
self.name = name
class UserKeyword(Base):
__tablename__ = 'user_keyword'
user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
keyword_id = Column(Integer, ForeignKey('keyword.id'), primary_key=True)
special_key = Column(String(50))
# bidirectional attribute/collection of "user"/"user_keywords"
user = relationship(User,
backref=backref("user_keywords",
cascade="all, delete-orphan")
)
# reference to the "Keyword" object
keyword = relationship("Keyword")
def __init__(self, keyword=None, user=None, special_key=None):
self.user = user
self.keyword = keyword
self.special_key = special_key
class Keyword(Base):
__tablename__ = 'keyword'
id = Column(Integer, primary_key=True)
keyword = Column('keyword', String(64))
def __init__(self, keyword):
self.keyword = keyword
def __repr__(self):
return 'Keyword(%s)' % repr(self.keyword)
Am I on the right track in following this pattern here?
My goal is essentially many-to-many with an extra column containing a boolean value.
This should work:
for user in users:
for keyword in user.user_keywords:
print keyword.special_key

how to add default related record with sqlaclhemy (e.g. user belongs to group)

I have two models: AuthUser and AuthGroup, they are linked via a many2many relationship. By default I have at least 3 user "states":
unlogged -> no group
logged in -> users group
admin -> admin group
I would like to be sure that whenever a new user is added to the database, it is added to the users group too. Is there a way to get this functionality in the model defintion?
here are my table definitions
Base = declarative_base()
user_group_table = Table('auth_user_groups', Base.metadata,
Column('user_id', types.Integer(), \
ForeignKey('auth_users.id', onupdate='CASCADE', ondelete='CASCADE')),
Column('group_id', types.Integer(), \
ForeignKey('auth_groups.id', onupdate='CASCADE', ondelete='CASCADE'))
)
class AuthGroup(Base):
__tablename__ = 'auth_groups'
__table_args__ = {"sqlite_autoincrement": True}
id = Column(types.Integer(), primary_key=True)
name = Column(Unicode(80), unique=True, nullable=False)
created = Column(types.DateTime(), default=functions.now())
users = relation('AuthUser', secondary=user_group_table, \
backref='auth_groups')
def __repr__(self):
return u'%s' % self.name
def __unicode__(self):
return self.name
class AuthUser(Base):
__tablename__ = 'auth_users'
__table_args__ = {"sqlite_autoincrement": True}
id = Column(types.Integer(), primary_key=True)
login = Column(Unicode(80), default=u'', index=True)
username = Column(Unicode(80), default=u'', index=True)
_password = Column('password', Unicode(80), default=u'', index=True)
email = Column(Unicode(80), default=u'', index=True)
groups = relation('AuthGroup', secondary=user_group_table, \
backref='auth_users')
def _set_password(self, password):
self._password = bcrypt.hashpw(password, bcrypt.gensalt())
def _get_password(self):
return self._password
password = synonym('_password', descriptor=property(_get_password, \
_set_password))
#classmethod
def get_by_id(cls, id):
return DBSession.query(cls).filter(cls.id==id).first()
#classmethod
def get_by_login(cls, login):
return DBSession.query(cls).filter(cls.login==login).first()
#classmethod
def get_by_username(cls, username):
return DBSession.query(cls).filter(cls.username==username).first()
#classmethod
def get_by_email(cls, email):
return DBSession.query(cls).filter(cls.email==email).first()
#classmethod
def check_password(cls, **kwargs):
if kwargs.has_key('id'):
user = cls.get_by_id(kwargs['id'])
if kwargs.has_key('username'):
user = cls.get_by_username(kwargs['username'])
if not user:
return False
if bcrypt.hashpw(kwargs['password'], user.password) == user.password:
return True
else:
return False
usually a class constructor validates expected data upon new object creation (an INSERT in the ORM, short of any trickery to bypass the usual mechanics, always corresponds to a new instance of a mapped class created via constructor):
class AuthUser(Base):
def __init__(self, **kw):
self.groups = kw.pop('groups', None)
if not self.groups:
raise ValueError("at least one group is required!")
super(AuthUser, self).__init__(**kw)