Skip to content

Commit 57d7a05

Browse files
committed
Fix InsertRelation on attached database
1 parent fd1c34f commit 57d7a05

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/duckdb_py/pyrelation.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "duckdb/common/arrow/physical_arrow_collector.hpp"
2424
#include "duckdb_python/arrow/arrow_export_utils.hpp"
2525

26+
#include <duckdb/main/relation/table_relation.hpp>
27+
2628
namespace duckdb {
2729

2830
DuckDBPyRelation::DuckDBPyRelation(shared_ptr<Relation> rel_p) : rel(std::move(rel_p)) {
@@ -1511,7 +1513,7 @@ DuckDBPyRelation &DuckDBPyRelation::Execute() {
15111513
void DuckDBPyRelation::InsertInto(const string &table) {
15121514
AssertRelation();
15131515
auto parsed_info = QualifiedName::Parse(table);
1514-
auto insert = rel->InsertRel(parsed_info.schema, parsed_info.name);
1516+
auto insert = rel->InsertRel(parsed_info.catalog, parsed_info.schema, parsed_info.name);
15151517
PyExecuteRelation(insert);
15161518
}
15171519

@@ -1565,14 +1567,24 @@ void DuckDBPyRelation::Update(const py::object &set_p, const py::object &where)
15651567

15661568
void DuckDBPyRelation::Insert(const py::object &params) {
15671569
AssertRelation();
1568-
if (!IsAcceptedInsertRelationType(*this->rel)) {
1570+
if (this->rel->type != RelationType::TABLE_RELATION) {
15691571
throw InvalidInputException("'DuckDBPyRelation.insert' can only be used on a table relation");
15701572
}
15711573
vector<vector<Value>> values {DuckDBPyConnection::TransformPythonParamList(params)};
15721574

15731575
D_ASSERT(py::gil_check());
15741576
py::gil_scoped_release release;
1575-
rel->Insert(values);
1577+
// Grab table info
1578+
auto table_relation = static_cast<TableRelation *>(this->rel.get());
1579+
auto catalog = table_relation->description->database;
1580+
auto schema = table_relation->description->schema;
1581+
auto table = table_relation->description->table;
1582+
// Create a value relation
1583+
vector<string> column_names;
1584+
auto value_rel =
1585+
make_shared_ptr<ValueRelation>(this->rel->context->GetContext(), values, std::move(column_names), "values");
1586+
// Now insert
1587+
value_rel->Insert(catalog, schema, table);
15761588
}
15771589

15781590
void DuckDBPyRelation::Create(const string &table) {

0 commit comments

Comments
 (0)