sqlalchemy

sqlalchemy func.group_concat and random order of data

坚强是说给别人听的谎言 提交于 2021-01-29 08:04:21
问题 I have a following query which is using func.group_concat to group some data r = aliased(Rank) t = aliased(Team) p = aliased(Participant) players_ranks_data = ( db.session.query( p, r.id.label('player_team_skill_id'), r.name.label('player_team_skill_name'), func.group_concat(t.name), ) .outerjoin(r, r.id == p.team_skill) .outerjoin(t, t.id == p.team_id) .filter(p.event_id == event_data.id) .filter(p.team_id != '') .group_by('player_team_skill_name') .all() ) When I'm printing what was

sqlalchemy func.group_concat and random order of data

流过昼夜 提交于 2021-01-29 07:59:11
问题 I have a following query which is using func.group_concat to group some data r = aliased(Rank) t = aliased(Team) p = aliased(Participant) players_ranks_data = ( db.session.query( p, r.id.label('player_team_skill_id'), r.name.label('player_team_skill_name'), func.group_concat(t.name), ) .outerjoin(r, r.id == p.team_skill) .outerjoin(t, t.id == p.team_id) .filter(p.event_id == event_data.id) .filter(p.team_id != '') .group_by('player_team_skill_name') .all() ) When I'm printing what was

One or more mappers failed to initialize. Could not locate any relevant foreign key columns for primary join condition

好久不见. 提交于 2021-01-29 05:26:47
问题 I have tried to implement this code from sqlalchemy import Column, ForeignKey, Integer, String, ForeignKeyConstraint from sqlalchemy_continuum import make_versioned , version_class, parent_class from sqlalchemy.ext.declarative import declarative_base import sqlalchemy as sa params = urllib.parse.quote_plus \ (r'Driver={ODBC Driver 17 for SQL Server};Server=HP;Database=Navigation;Uid=sa;Pwd=<added>;Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;') conn_str = 'mssql+pyodbc:///

How to make null/none count 0 instead?

大城市里の小女人 提交于 2021-01-29 05:02:17
问题 I have some sqlalchemy that counts rows with the value "3" and then prints out the count. It works fine if the count is 1, 2, 3, etc, but when there are no items, it prints nothing. I wanted it to print 0. So I added an if/else statement that didn't work (it prints nothing, no errors). How would I do this? Here's my current code — not really sure if I should be doing this part in sqlalchemy or python. q = (db.session.query(Article.snippet, sa.func.count(ArticleVote.id)) .join(ArticleVote,

Cannot update table via sqlalchemy

时光毁灭记忆、已成空白 提交于 2021-01-29 02:47:30
问题 I'm trying to update a row after a select instance = session.query(Scouts).filter(Scouts.camp == camp, Scouts.user == user,Scouts.gender == gender).first() instance.number_of_visits += 1 session.commit() My Scouts class looks like: class Scouts(Base): __tablename__ = 'scouts' camp = Column(String, primary_key=True) user = Column(Integer) gender = Column(String) number_of_visits = Column(Integer) def __init__(self, camp, user, gender, number_of_visits): self.camp = camp self.user = user self

Cannot update table via sqlalchemy

霸气de小男生 提交于 2021-01-29 02:45:29
问题 I'm trying to update a row after a select instance = session.query(Scouts).filter(Scouts.camp == camp, Scouts.user == user,Scouts.gender == gender).first() instance.number_of_visits += 1 session.commit() My Scouts class looks like: class Scouts(Base): __tablename__ = 'scouts' camp = Column(String, primary_key=True) user = Column(Integer) gender = Column(String) number_of_visits = Column(Integer) def __init__(self, camp, user, gender, number_of_visits): self.camp = camp self.user = user self

SQLAlchemy automap - adding methods to automapped Model

我与影子孤独终老i 提交于 2021-01-29 02:04:13
问题 I have a preexisting database that I'm using with SQLAlchemy, so I'm using automap to get the Models from the database. What is the best way to add methods to these classes? For example, for a User class, I'd like to add methods such as verifying the password. Also, I'd like to add methods for flask-login (UserMixin) methods. 回答1: Specify your classes explicitly beforehand, and define your methods as you would normally: Base = automap_base() class User(Base): __tablename__ = 'user' def verify

SQLAlchemy automap - adding methods to automapped Model

这一生的挚爱 提交于 2021-01-29 01:59:45
问题 I have a preexisting database that I'm using with SQLAlchemy, so I'm using automap to get the Models from the database. What is the best way to add methods to these classes? For example, for a User class, I'd like to add methods such as verifying the password. Also, I'd like to add methods for flask-login (UserMixin) methods. 回答1: Specify your classes explicitly beforehand, and define your methods as you would normally: Base = automap_base() class User(Base): __tablename__ = 'user' def verify

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: items

ε祈祈猫儿з 提交于 2021-01-28 23:30:24
问题 I don't understand why I have this error. Please explain the error. I used official documentation. I run Pipenv virtual env: python 3.8.2 sqlalchemy 1.3.16 You can try run this code too. import enum from sqlalchemy import create_engine, Column, Integer, String, Enum from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///:memory:', echo=True) Base = declarative_base() Session = sessionmaker(bind=engine) session =

SqlAlchemy (1366, “Incorrect string value: '\\xB4\\xEB\\xC7\\xD1\\xB9\\xCE…' for column 'VARIABLE_VALUE' at row 484”)

99封情书 提交于 2021-01-28 23:26:44
问题 (1366, "Incorrect string value: '\xB4\xEB\xC7\xD1\xB9\xCE...' for column 'VARIABLE_VALUE' at row 484") This error occurs whenever I try to insert a row (for anytable) But I don't have any table which contains 'VARIABLE_VAULE' column. SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('VARIABLE_VALUE') AND TABLE_SCHEMA='sw'; /* nothing comes up */ How do I locate the table which causes the warning? 回答1: The error happens because you are trying to store an emoji