我正在尝试实现以下查询来处理SQLAlchemy中的嵌套集(请参阅此处).我正在努力的是如何在最后一个子句depth中的主SELECT查询(这取决于子SELECT查询)中使用带标签的计算HAVING.
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
GROUP BY node.name
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name …Run Code Online (Sandbox Code Playgroud) 我正在考虑按照数据透视表上定义的顺序显示asset属于 a 的 s列表。在这种情况下,该功能没有任何作用。不会引发错误,但无论如何它都会以相同的顺序返回集合数组。productorder_columnproduct_assetsortBy
以下是我目前列出的内容:
数据库:
Schema::create('product_asset', function (Blueprint $table) {
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('asset_id')->unsigned()->index();
$table->foreign('asset_id')->references('id')->on('assets')->onDelete('cascade');
$table->unsignedInteger('order_column')->nullable();
});
Run Code Online (Sandbox Code Playgroud)
模型:
/**
* The assets that belong to the product.
*/
public function assets()
{
return $this->belongsToMany('App\Asset', 'product_asset')->withPivot('order_column');
}
Run Code Online (Sandbox Code Playgroud)
看法:
<ul>
@foreach($resources->sortBy('pivot_order_column') as $resource)
<li><a href="{{ $resource->url }}">{{ $resource->name }}</a></li>
@endforeach
</ul>
Run Code Online (Sandbox Code Playgroud)
我将非常感谢对此的任何帮助!提前致谢。
类似问题:
https://laravel.io/forum/04-17-2014-order-by-pivot-table-attribute-in-eloquent
我正在使用Pyramid框架并使用Deform包来呈现HTML表单,并给出了漏勺方案.我正在努力解决如何处理具有多对多关系的模式的问题.例如,我的sqlalchemy模型如下所示:
class Product(Base):
""" The SQLAlchemy declarative model class for a Product object. """
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
name = Column(String(80), nullable=False)
description = Column(String(2000), nullable=False)
categories = relationship('Category', secondary=product_categories,
backref=backref('categories', lazy='dynamic'))
class Category(Base):
""" The SQLAlchemy declarative model class for a Category object. """
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String(80), nullable=False)
products = relationship('Product', secondary=product_categories,
backref=backref('products', lazy='dynamic'))
product_categories = Table('product_categories', Base.metadata,
Column('products_id', Integer, ForeignKey('products.id')),
Column('categories_id', Integer, ForeignKey('categories.id'))
)
Run Code Online (Sandbox Code Playgroud)
如您所见,这是一个非常简单的模型,代表一个在线商店,其中一个产品可以属于一个或多个类别.在我的渲染形式中,我想有一个选择多个字段,我可以在其中选择几个不同的类别来放置产品.这是一个简单的漏勺模式:
def …Run Code Online (Sandbox Code Playgroud) 我有一个product与多对多关系的模型,product_categories如下所述:
class Product(Base):
""" The SQLAlchemy declarative model class for a Product object. """
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
part_number = Column(String(10), nullable=False, unique=True)
name = Column(String(80), nullable=False, unique=True)
description = Column(String(2000), nullable=False)
categories = relationship('Category', secondary=product_categories,
backref=backref('categories', lazy='dynamic'))
class Category(Base):
""" The SQLAlchemy declarative model class for a Category object. """
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
lft = Column(Integer, nullable=False)
rgt = Column(Integer, nullable=False)
name = Column(String(80), nullable=False)
description = Column(String(2000), …Run Code Online (Sandbox Code Playgroud)