小编luk*_*kik的帖子

SQLAlchemy将多个外键在一个映射类中转换为相同的主键

我试图设置一个postgresql表,它有两个指向另一个表中相同主键的外键.

当我运行脚本时,我收到错误

sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系Company.stakeholder上的父/子表之间的连接条件 - 有多个链接表的外键路径.指定'foreign_keys'参数,提供应列为包含对父表的外键引用的列的列表.

这是SQLAlchemy文档中的确切错误,但当我复制它们提供的解决方案时,错误不会消失.我能做错什么?

#The business case here is that a company can be a stakeholder in another company.
class Company(Base):
    __tablename__ = 'company'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)

class Stakeholder(Base):
    __tablename__ = 'stakeholder'
    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey('company.id'), nullable=False)
    stakeholder_id = Column(Integer, ForeignKey('company.id'), nullable=False)
    company = relationship("Company", foreign_keys='company_id')
    stakeholder = relationship("Company", foreign_keys='stakeholder_id')
Run Code Online (Sandbox Code Playgroud)

我在这里看到过类似的问题,但有些答案建议人们primaryjoin在文档中使用a 表明你primaryjoin在这种情况下不需要.

python postgresql sqlalchemy

37
推荐指数
2
解决办法
3万
查看次数

suds安装错误:没有名为client的模块

尝试使用pip-3.2安装suds并且它因错误而失败

sudo pip-3.2 install suds
Downloading/unpacking suds
  Running setup.py egg_info for package suds
  Traceback (most recent call last):
  File "<string>", line 16, in <module>
  File "/tmp/pip-build/suds/setup.py", line 20, in <module>
    import suds
  File "suds/__init__.py", line 154, in <module>
    import client
ImportError: No module named client
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 16, in <module>

File "/tmp/pip-build/suds/setup.py", line 20, in <module>

import suds

File "suds/__init__.py", line 154, in <module>

import client

ImportError: …
Run Code Online (Sandbox Code Playgroud)

python pip suds

32
推荐指数
2
解决办法
6万
查看次数

使用WHERE子句更新语句,该子句包含具有空值的列

我正在使用另一个表中的数据更新一个表上的列.该WHERE子句基于多个列,其中一些列为null.根据我的想法,这个空值是throwing off你的标准UPDATE TABLE SET X=Y WHERE A=B陈述.

请参阅两个表的SQL Fiddle,其中我尝试table_one根据来自的数据进行更新table_two.我的查询目前看起来像这样:

UPDATE table_one SET table_one.x = table_two.y 
FROM table_two
WHERE 
table_one.invoice_number = table_two.invoice_number AND
table_one.submitted_by = table_two.submitted_by AND
table_one.passport_number = table_two.passport_number AND
table_one.driving_license_number = table_two.driving_license_number AND
table_one.national_id_number = table_two.national_id_number AND
table_one.tax_pin_identification_number = table_two.tax_pin_identification_number AND
table_one.vat_number = table_two.vat_number AND
table_one.ggcg_number = table_two.ggcg_number AND
table_one.national_association_number = table_two.national_association_number
Run Code Online (Sandbox Code Playgroud)

对于某些行的查询失败,table_one.x在任何一个表中的任何列都没有得到更新时null.即只有当所有列都有一些数据时才会更新.

这个问题与我早些时候在SO上的问题有关,那里我从大型数据集中获取了不同的值Distinct On.我现在想要的是使用具有唯一字段的表中的值填充大数据集. …

sql postgresql sql-update

9
推荐指数
1
解决办法
7358
查看次数

验证 Swagger API 文档 (drf-yasg)

我已经设置了DRF-YASG,但无法弄清楚如何配置它以显示需要身份验证的视图。

下面是配置。

  schema_view = get_schema_view(
      openapi.Info(
        title="Swagger Doc",
        default_version='v1',
        description="Test description",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="contact@snippets.local"),
        license=openapi.License(name="BSD License"),
      ),
      validators=['flex', 'ssv'],
      permission_classes=(permissions.AllowAny,),  # If I change the permission it throws an exception. See below
      public=False,
      patterns=public_apis,
  )
Run Code Online (Sandbox Code Playgroud)

这些public_apis是我希望人们在通过身份验证后看到的 API。

使用上述配置,它不会显示单个 API。它只显示AuthorizeButton 和显示No operations defined in spec!. 但是如果我更改public=Falsepublic=True那么它会显示所有的 API。

PS:之前我使用的是Django Rest Swagger,并且我已经能够将它配置为仅在提供 JWT 令牌后才显示 API。

我正在使用 JWT 进行身份验证。

权限变更的例外:

另一个问题是,如果我将上面的权限更改为 DRF 权限类,渲染将失败并显示以下错误:

  Internal Server Error: /swagger/
  Traceback (most recent call …
Run Code Online (Sandbox Code Playgroud)

django django-rest-framework drf-yasg

9
推荐指数
1
解决办法
7764
查看次数

如何在 SQLAlchemy Core 中将列名作为参数传递?

我有一个 sqlalchemy 核心批量更新查询,我需要以编程方式传递要更新的列的名称。

该函数如下所示,每个变量都有注释:

def update_columns(table_name, pids, column_to_update):
    '''
    1. table_name: a string denoting the name of the table to be updated
    2. pid: a list of primary ids
    3. column_to_update: a string representing the name of the column that will be flagged. Sometimes the name can be is_processed or is_active and several more other columns. I thus need to pass the name as a parameter.
    '''
    for pid in pids:
        COL_DICT_UPDATE = {}
        COL_DICT_UPDATE['b_id'] = pid
        COL_DICT_UPDATE['b_column_to_update'] = True …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy python-3.x

6
推荐指数
1
解决办法
5715
查看次数

如何在 NOT IN 语句中使用 Postgresql ANY 运算符

使用 Pyscopg2,如何使用 ANY 运算符将 Python 列表传递到 SQL 语句中?

正常工作的 SQL 读取(请参阅 SQL Fiddle):

SELECT * FROM student WHERE id NOT IN (3);
Run Code Online (Sandbox Code Playgroud)

使用 Psycopg2 如下:

Psycopg2:查询 1

下面的查询失败 psycopg2.ProgrammingError: syntax error at or near "ANY"

id_list = [2,3,4]
cursor.execute("SELECT * FROM student WHERE id NOT IN ANY(%s)) %(id_list); 
Run Code Online (Sandbox Code Playgroud)

Psycopg2:查询 2

下面的查询不会引发错误,但会给出错误的结果,因为它不排除列表中的 ID。它表现得好像它是一个等于运算符,或者像它的IN语句一样具体,而我想要一个NOT IN

id_list = [2,3,4]
cursor.execute("SELECT * FROM student WHERE id != ANY(%s)), (id_list,); 
Run Code Online (Sandbox Code Playgroud)

另外,在我的搜索中,我遇到了pyscopg2 扩展 SQL_IN。在这种情况下可以使用吗?如果是这样,我该如何使用它?

python postgresql psycopg2

6
推荐指数
1
解决办法
4892
查看次数

使用主管跑花

我有使用主管开始花的挑战.

我的开发环境中的以下命令适用于控制台

celery --app=celery_conf.celeryapp flower --conf=flowerconfig

但转向生产使用主管我遇到了各种各样的错误

/supervisor/conf.d/flower.conf

[program:flower]
command=/opt/apps/venv/my_app/bin/celery flower --app=celery_conf.celeryapp --conf=flowerconfig
directory=/opt/apps/my_app
user=www-data
autostart=true
autorestart=false
redirect_stderr=true
stderr_logfile=/var/log/celery/flower.err.log
stdout_logfile=/var/log/celery/flower.out.log
Run Code Online (Sandbox Code Playgroud)

使用上面的配置,没有错误,但所有芹菜都给我一个像输出的帮助.它喜欢它不承认传递的变量.

 Type 'celery <command> --help' for help using a specific command.
 Usage: celery <command> [options]
 Show help screen and exit.
 Options:
   -A APP, --app=APP     app instance to use (e.g. module.attr_name)
   -b BROKER, --broker=BROKER
                         url to broker.  default is 'amqp://guest@localhost//'
   --loader=LOADER       name of custom loader class to use.
   etc..
   etc..
   etc...
Run Code Online (Sandbox Code Playgroud)

另一方面,主管抛出 INFO exited: flower (exit status 64; not expected)

我有其他主管启动的应用程序使用 …

python celery flower

6
推荐指数
1
解决办法
3186
查看次数

Oursql insallation失败,"cython not found"

试图在ubuntu 12.10上安装python3x和sqlalchemy0.8的myql驱动程序.它失败并出现以下错误.

sudo pip-3.2 install oursql
Downloading/unpacking oursql
Running setup.py egg_info for package oursql
Traceback (most recent call last):
  File "<string>", line 16, in <module>
  File "/tmp/pip-build/oursql/setup.py", line 53
    print "cython not found, using previously-cython'd .c file."
                                                               ^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 16, in <module>

File "/tmp/pip-build/oursql/setup.py", line 53

print "cython not found, using previously-cython'd .c file."

                                                           ^

SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

当我尝试安装cython时,我似乎已经拥有它:

sudo pip-3.2 …
Run Code Online (Sandbox Code Playgroud)

python mysql sqlalchemy oursql

5
推荐指数
1
解决办法
4548
查看次数

如何使用sqlalchemy将初始数据加载到数据库中

我希望能够在使用SQLAlchemy创建表时自动加载数据.

在django中,您可以使用fixtures,在创建表时,可以使用数据轻松预填充数据库.这个我觉得很有用,特别是当你有基本的"查找"表时,例如product_type,student_type只包含几行甚至是一个像货币一样的表,这些货币将加载世界上的所有货币而不必一次又一次地键入它们.你摧毁你的模特/班级.

我目前的应用程序不使用django.我有SQLAlchemy.我怎样才能实现同样的目标?我希望应用程序知道数据库是第一次创建,因此它会使用数据填充一些表.

python django sqlalchemy

5
推荐指数
2
解决办法
2614
查看次数

如何清除listproxy中的内容

如何清除共享的多进程manager.list?在下面的示例中,我想在循环继续之前清除它,以便新生成的进程找到一个空列表.

num_consumers = multiprocessing.cpu_count() 
p = multiprocessing.Pool(num_consumers)
manager = multiprocessing.Manager()
mp_list = manager.list()

def put_some_data(data):
    #Processing occurs and then we append the result
    mp_list.append(data)

def do_some_processing():
    While True:
        #Multiprocessing runs here
        result = p.map(put_some_data, data)
        mp_list.clear()
        #When done, break
Run Code Online (Sandbox Code Playgroud)

以上抛出错误 AttributeError: 'ListProxy' object has no attribute 'clear'

关于如何清除proxylist对象的文档不是很清楚()

python multiprocessing

5
推荐指数
1
解决办法
2168
查看次数