- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
按照 sqlalchemy documentation and adapting 中的示例,我的数据库有这段代码:
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import create_engine, MetaData
engine = create_engine('postgresql://u:pw@db')
metadata = MetaData(schema='name')
metadata.reflect(engine, only=['a', 'b'])
Base = automap_base(bind=engine, metadata=metadata)
Base.prepare()
A, B = Base.classes.a, Base.classes.b
result = A.first()
print(result.id)
运行脚本返回此错误:
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/<User>/untitled0.py", line 18, in <module>
Base.prepare(engine,reflect=True)
File "/anaconda3/lib/python3.6/site-packages/sqlalchemy/ext/automap.py", line 788, in prepare
generate_relationship)
File "/anaconda3/lib/python3.6/site-packages/sqlalchemy/ext/automap.py", line 895, in _relationships_for_fks
local_cls, referred_cls):
TypeError: issubclass() arg 2 must be a class or tuple of classes
最佳答案
由于可以在没有模式的情况下识别表,因此消除限制然后使用元数据在没有声明基础的情况下自动映射。
以下是相关 sqlalchemy 文档的链接:
from sqlalchemy import create_engine, MetaData, Table, select
engine = create_engine('postgresql://u:pw@db')
metadata = MetaData(engine)
a = Table('a', metadata, autoload=True, autoload_with=engine)
b = Table('b', metadata, autoload=True, autoload_with=engine)
ab = a.join(b)
query = select([a.c.id,b.c.id]).select_from(ab)
result = conn.execute(query).fetchone()
运行此脚本将返回表 a 的第一条记录和表 b 的第一条连接记录。
请注意,此格式完全依赖于两个表之间具有外键连接的数据库。此外,在大多数编辑器中,使用这些类将为您提供 table.fields 的代码完成,而此方法不会。
关于python-3.x - Postgres SQLalchemy automap 无法识别关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50803680/
我正在为类 A 使用 AutoMapper和 B .该项目是一个 .Net 核心项目 enabled . public class A { public ClassX? X { get; se
这是我遇到的错误 CookBook.Tests.CategoryRepository_Fixture.Can_update_category: FluentNHibernate.Cfg.FluentC
我有一个关于使用 PrimaryKeyNamingConvention 的问题 假设有以下类: public class banco { [Required] public virtu
我有一个与 SQLAlchemy 一起使用的预先存在的数据库,因此我使用 automap 从数据库中获取模型。向这些类添加方法的最佳方法是什么?例如,对于 User 类,我想添加验证密码等方法。另外,
我是 SQLAlchemy(通常是 ORM)的新手,我正在尝试将现有的应用程序转移到 SQLAlchemy,以便我们可以将一些代码复杂性从当前现有(并且更新乏味)查询转移到 Python。不幸的是,我
我正在使用具有自动映射功能的 SQL Alchemy v(0.9.1)。这允许我自动创建类和关系。 http://docs.sqlalchemy.org/en/rel_0_9/orm/extensio
鉴于以下类(class): public class Address : Place { public virtual string Street { get; set; } publ
我是 Fluent 的初学者,我遇到了一个问题,这可能是我自己的错。 我有一个包含 3 个类的命名空间。实体、实体版本和属性。类之间不涉及继承。 我尝试只映射 Entity 类,但实际情况是命名空间中
我正在使用 fluent nhibernate 来映射以下模型: public abstract class BasePermission : Entity { public abstract
我正在尝试 Fluent NHibernate 的自动映射功能,当我尝试移动时,构建 SessionFactory 时使用显式 ClassMap 配置的相同代码失败它自动映射。 代码如下: publi
NHibernate 似乎不能自动映射一个实体中给定类型的多个 IList。 考虑以下两个实体(基于 Fluent NHibernate 源代码中包含的 Examples.FirstProject 示
我在使用 CultureInfo 属性自动映射实体时遇到问题,当我尝试构建 SessionFactory 时,它抛出异常并出现以下错误:“来自表 ExampleClass 的关联引用了一个未映射的类:
我有两张 table ,地点和设施 他们映射到两个类, public Location : Entity { //properties } public Facility : Entity {
我是 NHibernate 和流利的 nhibernate 新手。我在唯一约束和 nhibernate 映射方面遇到了一些问题。 我有领域模型的以下部分。 public class Batch {
当数据对象模型发生变化(向类添加新属性)时,流畅的自动映射是否有一种方法可以改变模式,目前它只会删除并重新创建数据库模式,这将丢失数据库中的所有数据.它可以使用 Alter table 而不是 dro
给定以下模型: using NetTopologySuite.Geometries; public class bounding_box { public virtual int id { g
我也是 NHibernate 和 FNH 的新手。我熟悉 ORM,并决定看看所有关于这个特定 ORM 的讨论主要是因为生产力的提高。在这一点上,我认为我的时间最好花在其他东西上,但我不想让它打败我,我
我需要做的是我的抽象页面类的自动映射子类。我需要在我获得运行时(在初始化阶段)的程序集列表中找到它们。我没有对我的映射程序集中的程序集的任何引用。 我的页面类看起来像这样: public abstra
有人可以帮忙吗,我如何指示 automap 为 not-null 一个专栏? public class Paper : Entity { public Paper() { }
如果使用 Fluent NHibernate,是否可以自动映射大多数类,但指定应该使用常规 fluent API 映射几个特定类而不是自动映射?如果是这样,任何人都可以指点我一些示例代码来展示如何做到
我是一名优秀的程序员,十分优秀!