简体   繁体   English

定义没有外键的关系

[英]Define relationship without foreign key

How can I make a relationship without having a foreign key? 如何在没有外键的情况下建立关系?

@declared_attr 
def custom_stuff(cls): 
    joinstr = 'foreign(Custom.name) == "{name}"'.format(name=cls.__name__)  
    return db.relationship('Custom', primaryjoin=joinstr) 

This raises an error: 这引发了一个错误:
ArgumentError: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition

This works, but I think it's a pretty ugly hack. 这有效,但我认为这是一个非常丑陋的黑客。

@declared_attr
def custom_stuff(cls):
    joinstr = 'or_(
                  and_(foreign(Custom.name) == MyTable.title, 
                       foreign(Custom.name) != MyTable.title), 
                  foreign(Custom.name) == "{name}")'.format(name=cls.__name__)
    return db.relationship('Custom', primaryjoin=joinstr)

Is there a better way to do this? 有一个更好的方法吗?

EDIT: the extra attribute needs to be added as @declared_attr and has to use a relationship, since our serializer is written so it works with declarred attrs. 编辑:额外属性需要添加为@declared_attr并且必须使用关系,因为我们的序列化程序是编写的,因此它适用于声明的attrs。

Doing this with @hybrid_property or something else would work, but then our json serializer would break. 使用@hybrid_property或其他方法执行此@hybrid_property会起作用,但随后我们的json序列化程序将会中断。 Getting that to work seems harder than defining a relationship. 让它发挥作用似乎比定义关系更难。

You don't necessarily have to define relationships when creating tables in a database (this applies for almost every SQL). 在数据库中创建表时,您不一定要定义关系(这几乎适用于每个SQL)。 You can still join tables that don't have a foreign-key relation predefined (the main reason for foreign keys is to enforce data consistency, not to define what you can join or not). 您仍然可以连接没有预定义外键关系的表(外键的主要原因是强制数据一致性,而不是定义您可以加入的内容)。

See this for reference - http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.join 请参阅此参考 - http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.join

If you would still like to "show" the database and table structure/model, then better use some entity relationship modeler like ERwin (or some diagramming software). 如果你仍然想“显示”数据库和表结构/模型,那么最好使用像ERwin(或某些图表软件)这样的实体关系建模器。

Maybe you should point to the meaning of "relationship" word. 也许你应该指出“关系”这个词的含义。 It states that some thing "depends" on some other thing or both depend each other. 它指出某些东西“取决于”其他东西或两者都相互依赖。 If you're trying to define a relationship on the ERM (Entity Relationship Model) that means you should state which one of the entities "depends" on the other one. 如果您尝试在ERM(实体关系模型)上定义关系,则意味着您应该说明哪个实体“依赖”另一个实体。 Also, databases have some hacks to deal faster with tables relating each other than just simple tables that are related on an upper abstract way. 此外,数据库有一些黑客可以更快地处理彼此相关的表,而不仅仅是与上层抽象方式相关的简单表。 Is there any reason why you need to do that? 你有什么理由要这么做吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM