天道酬勤,学无止境

sqlalchemy

SQL Alchemy : Table primary_key - unexpected keyword argument error

I keep getting the below error regarding the primary_key=True . Please could someone point out where i'm going wrong. I'm trying to follow: http://docs.sqlalchemy.org/en/latest/core/metadata.html#metadata-describing from sqlalchemy import * mydata123 = table("dailymarketdata", metadata, column('data_id', mysql.BIGINT, primary_key=True), column('dt', Date), column('security_id', mysql.VARCHAR), column('open', float), column('high', float), column('low', float), column('close', float), column('ma200', float), column('rtrend', mysql.TINYINT)) TypeError Traceback (most recent call last) <ipython

2022-01-17 06:00:37    分类:问答    python   mysql   sqlalchemy   keyword

how to query inside a class in sqlalchemy

I have a User class with a one-to-many relationship to the Item class: class User(Base): items=relationship(Item) def method(self): for item in self.items if self.items.itemname=='my item' #do something Now I want to get access of some of the items (filtered) of a User in a method inside the User class. Is it more efficient (performance wise) to write a for-loop running over all the items (as it is in the example) or to run a query inside the class? (let's say there are a couple of thousands of items for an average user). I also don't know how I can even run a query inside a class! Is there

2022-01-17 05:41:58    分类:问答    sqlalchemy

How to use an equivalent to __post_init__ method with normal class?

I would like to store entity used in my code and avoid multiple occurrences. Thus, my idea was to use an __init__ method for collecting the main data for my class, and then use a kind of __post_init__ method for computing an id from my class object. Here is the code: class Worker(Base): __tablename__='worker' id = Column(Integer,primary_key=True) profile=Column(String(100),nullable=False) useragent=Column(String(100),nullable=False) def __init__(self,useragent,profile): """ specify the main information""" print('init') self.profile= profile self.useragent=useragent def __post_init__(self): """

2022-01-17 05:05:09    分类:问答    python   sqlalchemy

What does an Alembic revision ID represent?

I have just started looking at Alembic, and coming from Django, where we have South to migrate our database schemas (which is soon to be included) which uses a friendly old fixed-width number like 0037_fix_my_schema.py to talk about the order in which migrations are to be applied, I am naturally intrigued by Alembic's revision ID. Is there a DAG backing Alembic, or can someone give a little overview of its internals in this respect?

2022-01-17 02:42:02    分类:问答    sqlalchemy   django-south   revision   directed-acyclic-graphs   alembic

how to execute LIKE query in sqlalchemy?

I am using sqlalchemy to fetch data from tables. Right now fetching all records from a table called audit_trail_table is working as expected. select_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.id == int(id)) row = conn.execute(select_stmt).fetchone() Now I wish to execute LIKE queries on multiple columns as follows: filter_query = #(some value which will work as a filter_query while fetching )records filter_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.first_name like '%' + filter_query + '%' or self.audit_trail_table.c.last_name like '%')

2022-01-16 23:04:37    分类:问答    python   mysql   sql   select   sqlalchemy

Why is SQLAlchemy/associationproxy duplicating my tags?

I'm trying to use association proxy for tags, in a very similar scenario to the example in the docs. Here is a subset of my schema (it's a blog), using declarative: class Tag(Base): __tablename__ = 'tags' id = Column(Integer, primary_key=True) tag = Column(Unicode(255), unique=True, nullable=False) class EntryTag(Base): __tablename__ = 'entrytags' entry_id = Column(Integer, ForeignKey('entries.id'), key='entry', primary_key=True) tag_id = Column(Integer, ForeignKey('tags.id'), key='tag', primary_key=True) class Entry(Base): __tablename__ = 'entries' id = Column(Integer, primary_key=True)

2022-01-16 18:38:46    分类:问答    python   orm   tags   sqlalchemy   declarative

Is SQLAlchemy/psycopg2 connection to PostgreSQL database encrypted

When I use SQLAlchemy with an external postgreSQL server, is the connection secured/encrypted? from sqlalchemy.engine import create_engine engine = create_engine('postgresql://scott:tiger@ip:5432/mydatabase') What about psycopg2?

2022-01-16 18:26:20    分类:问答    python   postgresql   sqlalchemy   psycopg2

sqlalchemy 身份映射问题(sqlalchemy identity map question)

问题 身份映射和工作单元模式是 sqlalchemy 比 django.db 更具吸引力的部分原因。 但是,我不确定身份映射如何工作,或者当应用程序配置为 wsgi 并且通过 api 调用而不是共享服务直接访问 orm 时它是否工作。 我想apache会为每个请求创建一个带有自己的python实例的新线程。 因此,每个实例都有自己的 sqlalchemy 类实例,并且无法使用身份映射。 这个对吗? 回答1 我认为您误解了身份映射模式。 来自:http://martinfowler.com/eaaCatalog/identityMap.html 身份映射记录了在单个业务事务中从数据库中读取的所有对象。 单个业务交易的记录保存在身份映射中。 这意味着无论您的 Web 服务器是如何配置的,您可能不会将它们保留超过请求的时间(或将它们存储在会话中)。 通常,您不会有很多用户参与单个业务交易。 无论如何,您可能不希望您的用户共享对象,因为他们最终可能会做一些自相矛盾的事情。 回答2 所以这一切都取决于你如何设置你的 sqlalchemy 连接。 通常你所做的是管理每个 wsgi 请求以拥有它自己的线程本地会话。 本次会议将了解它的所有情况,添加/更改/等项目。 但是,每个线程都不知道其他线程。 通过这种方式,模型和映射的加载/预配置在启动期间共享,但是每个请求都可以独立于其他请求运行。

2022-01-16 16:26:37    分类:技术分享    python   sqlalchemy   identity-map

如何在 SQL 数据库中以毫秒精度存储日期时间(How to store datetime with millisecond precision in SQL database)

问题 使用表示日期和时间的浮点值,精度为毫秒: import datetime float_time = 1485538757.29289 print datetime.datetime.fromtimestamp(float_time) 印刷: 2017-01-27 09:39:17.292890 要将其存储在数据库中: from sqlalchemy import Column, DateTime from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class MyTable(Base): __tablename__ = 'mytable' time_created = Column(DateTime, nullable=False) 但是保存的值向下舍入到2017-01-27 09:39:17 (从2017-01-27 09:39:17.292890 )。 有解决办法吗? 回答1 这取决于您使用的 SQL 数据库。 它们的精度不同: PostgreSQL :默认 1 微秒。 (有关远期/过去的警告,请参阅文档。) MySQL :默认 1 秒。 5.6.4 版本之后毫秒/微秒精度可选。 MariaDB :默认 1 秒。 从 5.3 版开始,毫秒/微秒精度可选。

2022-01-16 14:18:13    分类:技术分享    python   sql   sqlalchemy

attributeError: can't set attribute with flask-SQLAlchemy [duplicate]

This question already has answers here: Can't set attribute on result objects in SQLAlchemy flask (2 answers) Closed 2 years ago. I'm using Flask-SQLAlchemy version 2.1 which installs sqlalchemy version 1.x. My below code which first fetch an array of resultset and then loop over to modify an existing attribute used to work but now it does not. question_topic = Question.query.join(Topic).join(User,User.id==Question.user_id).add_columns(User.email,Question.question, Question.date, Topic.topic_name, Question.id, Topic.question_id)\ .filter(Question.id == Topic.question_id).all() for q_t in

2022-01-16 13:08:17    分类:问答    python   sqlalchemy   flask-sqlalchemy