基本上,我有一个对象belongs_to:companies,并具有:company_id属性.当我渲染json:@ coupons时,JSON是否可能包含其所有者的属性而不是company_id?
给定 SQLite 中的下表:
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
hair_color VARCHAR NOT NULL DEFAULT 'Green'
)
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行批量插入,hair_color仅对某些行使用默认值。例如,在 PostgreSQL 或 MySQL 中,它可能如下所示:
INSERT INTO users (name, hair_color) VALUES
('Sean', 'Black'),
('Tess', DEFAULT)
Run Code Online (Sandbox Code Playgroud)
但是,SQLite 不提供DEFAULT除 之外的任何其他上下文中的关键字DEFAULT VALUES。我知道我可以通过两个查询来完成此操作
INSERT INTO users (name, hair_color) VALUES ('Sean', 'Black');
INSERT INTO users (name) VALUES ('Tess');
Run Code Online (Sandbox Code Playgroud)
但是,我无法想象 SQLite 不提供某种机制来在单个查询中执行此操作。DEFAULTSQLite 其他后端的关键字是否有等效项?
在编写测试时,我希望能够在请求中注入连接,以便我可以将整个测试用例包装在事务中(即使测试用例中有多个请求).
我尝试使用BeforeMiddleware我可以在我的测试用例中链接的插入连接,如下所示:
pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>;
pub struct DatabaseOverride {
conn: DatabaseConnection,
}
impl BeforeMiddleware for DatabaseOverride {
fn before(&self, req: &mut Request) -> IronResult<()> {
req.extensions_mut().entry::<DatabaseOverride>().or_insert(self.conn);
Ok(())
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在尝试执行此操作时遇到编译错误:
error: the trait bound `std::rc::Rc<diesel::pg::connection::raw::RawConnection>: std::marker::Sync` is not satisfied [E0277]
impl BeforeMiddleware for DatabaseOverride {
^~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::rc::Rc<diesel::pg::connection::raw::RawConnection>` cannot be shared between threads safely
note: required because it appears within the type `diesel::pg::PgConnection`
note: required because …Run Code Online (Sandbox Code Playgroud)