Skip to content

Fix ambiguous column references in spatial queries #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/FluentPostGIS/Queries/QueryBuilder+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ extension QueryBuilder {
static func path<M, F>(_ field: KeyPath<M, F>) -> any SQLExpression
where M: Schema, F: QueryableProperty, F.Model == M
{
let path = M.path(for: field).map(\.description).joined(separator: "_")
return SQLColumn(path)
let schema = SQLIdentifier(M.schemaOrAlias)
let path = SQLIdentifier(M.path(for: field).map(\.description).joined(separator: "_"))
return SQLColumn(path, table: schema)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Tests/FluentPostGISTests/FluentPostGISTestCase.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FluentKit
import FluentPostgresDriver
import FluentPostGIS
import PostgresKit
import XCTest

Expand All @@ -11,7 +12,7 @@ class FluentPostGISTestCase: XCTestCase {
on: self.dbs.eventLoopGroup.next()
)!
}

override func setUp() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let threadPool = NIOThreadPool(numberOfThreads: 1)
Expand All @@ -25,6 +26,7 @@ class FluentPostGISTestCase: XCTestCase {
)
self.dbs.use(.postgres(configuration: configuration), as: .psql)

try await EnablePostGISMigration().prepare(on: self.db)
for migration in self.migrations {
try await migration.prepare(on: self.db)
}
Expand All @@ -34,6 +36,7 @@ class FluentPostGISTestCase: XCTestCase {
for migration in self.migrations {
try await migration.revert(on: self.db)
}
try await EnablePostGISMigration().revert(on: self.db)
}

private let migrations: [any AsyncMigration] = [
Expand All @@ -42,5 +45,6 @@ class FluentPostGISTestCase: XCTestCase {
UserPathMigration(),
UserAreaMigration(),
UserCollectionMigration(),
GuestMigration()
]
}
17 changes: 17 additions & 0 deletions Tests/FluentPostGISTests/QueryTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
@testable import FluentPostGIS
import FluentKit
import XCTest

final class QueryTests: FluentPostGISTestCase {
func testAlias() {
let query = Guest.query(on: self.db)
.join(Host.self, on: \Guest.$host.$id == \Host.$id)

for join in query.query.joins {
if case DatabaseQuery.Join.join(
schema: let schema,
alias: let alias?,
let method,
let foreign,
let local
) = join
{ XCTAssertTrue(!alias.isEmpty) }
}
}

func testContains() async throws {
let exteriorRing = GeometricLineString2D(points: [
GeometricPoint2D(x: 0, y: 0),
Expand Down
36 changes: 36 additions & 0 deletions Tests/FluentPostGISTests/TestModels.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import FluentKit
import FluentPostGIS



final class Host: ModelAlias {
static let name = "host"
let model = Guest()
}

final class Guest: Model, @unchecked Sendable {
static let schema: String = "guest"

@ID(custom: .id, generatedBy: .database)
var id: Int?

@OptionalParent(key: "host_id")
var host: Guest?

init() {}

init(hostID: Guest.IDValue? = nil) {
self.$host.id = hostID
}
}

struct GuestMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema(Guest.schema)
.field(.id, .int, .identifier(auto: true))
.field("host_id", .int, .references(Guest.schema, "id"))
.create()
}

func revert(on database: any Database) async throws {
try await database.schema(Guest.schema).delete()
}
}

final class UserLocation: Model, @unchecked Sendable {
static let schema = "user_location"

Expand Down
Loading