Git Product home page Git Product logo

Comments (9)

simolus3 avatar simolus3 commented on August 16, 2024 1

I've fixed this in 9873680, but we won't be able to to much better than applying some unhelpful suffix to avoid the name conflict.

You can define columns on the view itself, which allows specifying a custom name:

abstract class DynamicColumnsWithForeignKeys extends View {
  DynamicColumns get sourceDynamicColumns;
  DynamicColumns get targetDynamicColumns;
  DynamicTableForeignKeys get foreignKeys;

  Expression<int> get sourceId => sourceDynamicColumns.id;
  Expression<int> get sourceDynamicTableId => soure.dynamicTableId;

  @override
  Query as() => select([
        sourceId,
        sourceDynamicTableId,
        targetDynamicColumns.id,
        targetDynamicColumns.dynamicTableId,
        foreignKeys.id,
      ]).from(sourceDynamicColumns).join([
        leftOuterJoin(foreignKeys,
            foreignKeys.sourcePropertyId.equalsExp(sourceDynamicColumns.id)),
        leftOuterJoin(targetDynamicColumns,
            targetDynamicColumns.id.equalsExp(foreignKeys.targetPropertyId))
      ]);
}

You probably have to do the same for the target table and foreignKeys.id to resolve all conflicts manually.

from drift.

simolus3 avatar simolus3 commented on August 16, 2024 1

No problem, thanks for the detailed follow-up report. I've fixed that in f939ef1 - so the next drift version should work here. Also thanks for the sponsorship :)

from drift.

AlexandreAndrade00 avatar AlexandreAndrade00 commented on August 16, 2024

I will use your suggestion. Thanks for the fast reply and help! 😁

from drift.

AlexandreAndrade00 avatar AlexandreAndrade00 commented on August 16, 2024

Hello again @simolus3

I used your suggestion but in the generated code the attachedDatabase doesn't have the tables. (Sorry for the long snippet)

// view.dart

import 'package:drift/drift.dart';
import 'package:persistence/src/database/models/dynamic_database/dynamic_database.dart';

abstract class DynamicColumnsWithForeignKeys extends View {
  DynamicColumns get sourceDynamicColumns;
  DynamicColumns get targetDynamicColumns;
  DynamicTableForeignKeys get foreignKeys;

  Expression<BigInt> get sourceColumnId => sourceDynamicColumns.id;
  Expression<BigInt> get sourceTableId => sourceDynamicColumns.dynamicTableId;
  Expression<BigInt> get targetColumnId => targetDynamicColumns.id;
  Expression<BigInt> get targetTableId => targetDynamicColumns.dynamicTableId;
  Expression<BigInt> get foreignKeyId => foreignKeys.id;
  Expression<BigInt> get relationId => foreignKeys.relationId;

  @override
  Query as() => select([
        sourceColumnId,
        sourceTableId,
        targetColumnId,
        targetTableId,
        foreignKeyId,
        relationId,
      ]).from(sourceDynamicColumns).join([
        leftOuterJoin(foreignKeys,
            foreignKeys.sourcePropertyId.equalsExp(sourceDynamicColumns.id)),
        leftOuterJoin(targetDynamicColumns,
            targetDynamicColumns.id.equalsExp(foreignKeys.targetPropertyId))
      ]);
}

// view.drift.dart

// ignore_for_file: type=lint
import 'package:drift/drift.dart' as i0;
import 'package:persistence/src/database/views/dynamic_columns_with_foreign_keys.drift.dart'
    as i1;
import 'package:drift/src/runtime/query_builder/query_builder.dart' as i2;

class DynamicColumnsWithForeignKey extends i0.DataClass {
  final BigInt? sourceColumnId;
  final BigInt? sourceTableId;
  final BigInt? targetColumnId;
  final BigInt? targetTableId;
  final BigInt? foreignKeyId;
  final BigInt? relationId;
  const DynamicColumnsWithForeignKey(
      {this.sourceColumnId,
      this.sourceTableId,
      this.targetColumnId,
      this.targetTableId,
      this.foreignKeyId,
      this.relationId});
  factory DynamicColumnsWithForeignKey.fromJson(Map<String, dynamic> json,
      {i0.ValueSerializer? serializer}) {
    serializer ??= i0.driftRuntimeOptions.defaultSerializer;
    return DynamicColumnsWithForeignKey(
      sourceColumnId: serializer.fromJson<BigInt?>(json['sourceColumnId']),
      sourceTableId: serializer.fromJson<BigInt?>(json['sourceTableId']),
      targetColumnId: serializer.fromJson<BigInt?>(json['targetColumnId']),
      targetTableId: serializer.fromJson<BigInt?>(json['targetTableId']),
      foreignKeyId: serializer.fromJson<BigInt?>(json['foreignKeyId']),
      relationId: serializer.fromJson<BigInt?>(json['relationId']),
    );
  }
  @override
  Map<String, dynamic> toJson({i0.ValueSerializer? serializer}) {
    serializer ??= i0.driftRuntimeOptions.defaultSerializer;
    return <String, dynamic>{
      'sourceColumnId': serializer.toJson<BigInt?>(sourceColumnId),
      'sourceTableId': serializer.toJson<BigInt?>(sourceTableId),
      'targetColumnId': serializer.toJson<BigInt?>(targetColumnId),
      'targetTableId': serializer.toJson<BigInt?>(targetTableId),
      'foreignKeyId': serializer.toJson<BigInt?>(foreignKeyId),
      'relationId': serializer.toJson<BigInt?>(relationId),
    };
  }

  i1.DynamicColumnsWithForeignKey copyWith(
          {i0.Value<BigInt?> sourceColumnId = const i0.Value.absent(),
          i0.Value<BigInt?> sourceTableId = const i0.Value.absent(),
          i0.Value<BigInt?> targetColumnId = const i0.Value.absent(),
          i0.Value<BigInt?> targetTableId = const i0.Value.absent(),
          i0.Value<BigInt?> foreignKeyId = const i0.Value.absent(),
          i0.Value<BigInt?> relationId = const i0.Value.absent()}) =>
      i1.DynamicColumnsWithForeignKey(
        sourceColumnId:
            sourceColumnId.present ? sourceColumnId.value : this.sourceColumnId,
        sourceTableId:
            sourceTableId.present ? sourceTableId.value : this.sourceTableId,
        targetColumnId:
            targetColumnId.present ? targetColumnId.value : this.targetColumnId,
        targetTableId:
            targetTableId.present ? targetTableId.value : this.targetTableId,
        foreignKeyId:
            foreignKeyId.present ? foreignKeyId.value : this.foreignKeyId,
        relationId: relationId.present ? relationId.value : this.relationId,
      );
  @override
  String toString() {
    return (StringBuffer('DynamicColumnsWithForeignKey(')
          ..write('sourceColumnId: $sourceColumnId, ')
          ..write('sourceTableId: $sourceTableId, ')
          ..write('targetColumnId: $targetColumnId, ')
          ..write('targetTableId: $targetTableId, ')
          ..write('foreignKeyId: $foreignKeyId, ')
          ..write('relationId: $relationId')
          ..write(')'))
        .toString();
  }

  @override
  int get hashCode => Object.hash(sourceColumnId, sourceTableId, targetColumnId,
      targetTableId, foreignKeyId, relationId);
  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      (other is i1.DynamicColumnsWithForeignKey &&
          other.sourceColumnId == this.sourceColumnId &&
          other.sourceTableId == this.sourceTableId &&
          other.targetColumnId == this.targetColumnId &&
          other.targetTableId == this.targetTableId &&
          other.foreignKeyId == this.foreignKeyId &&
          other.relationId == this.relationId);
}

class $DynamicColumnsWithForeignKeysView extends i0.ViewInfo<
    i1.$DynamicColumnsWithForeignKeysView,
    i1.DynamicColumnsWithForeignKey> implements i0.HasResultSet {
  final String? _alias;
  @override
  final i0.GeneratedDatabase attachedDatabase;
  $DynamicColumnsWithForeignKeysView(this.attachedDatabase, [this._alias]);
  // NOT AVAILABLE
  $DynamicColumnsTable get sourceDynamicColumns =>
      attachedDatabase.dynamicColumns.createAlias('t0');
  // NOT AVAILABLE
  $DynamicColumnsTable get targetDynamicColumns =>
      attachedDatabase.dynamicColumns.createAlias('t1');
  // NOT AVAILABLE
  $DynamicTableForeignKeysTable get foreignKeys =>
      attachedDatabase.dynamicTableForeignKeys.createAlias('t2');
  @override
  List<i0.GeneratedColumn> get $columns => [
        sourceColumnId,
        sourceTableId,
        targetColumnId,
        targetTableId,
        foreignKeyId,
        relationId
      ];
  @override
  String get aliasedName => _alias ?? entityName;
  @override
  String get entityName => 'dynamic_columns_with_foreign_keys';
  @override
  Map<i0.SqlDialect, String>? get createViewStatements => null;
  @override
  $DynamicColumnsWithForeignKeysView get asDslTable => this;
  @override
  i1.DynamicColumnsWithForeignKey map(Map<String, dynamic> data,
      {String? tablePrefix}) {
    final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
    return i1.DynamicColumnsWithForeignKey(
      sourceColumnId: attachedDatabase.typeMapping.read(
          i0.DriftSqlType.bigInt, data['${effectivePrefix}source_column_id']),
      sourceTableId: attachedDatabase.typeMapping.read(
          i0.DriftSqlType.bigInt, data['${effectivePrefix}source_table_id']),
      targetColumnId: attachedDatabase.typeMapping.read(
          i0.DriftSqlType.bigInt, data['${effectivePrefix}target_column_id']),
      targetTableId: attachedDatabase.typeMapping.read(
          i0.DriftSqlType.bigInt, data['${effectivePrefix}target_table_id']),
      foreignKeyId: attachedDatabase.typeMapping.read(
          i0.DriftSqlType.bigInt, data['${effectivePrefix}foreign_key_id']),
      relationId: attachedDatabase.typeMapping
          .read(i0.DriftSqlType.bigInt, data['${effectivePrefix}relation_id']),
    );
  }

  late final i0.GeneratedColumn<BigInt> sourceColumnId =
      i0.GeneratedColumn<BigInt>('source_column_id', aliasedName, true,
          generatedAs: i0.GeneratedAs(sourceDynamicColumns.id, false),
          type: i0.DriftSqlType.bigInt);
  late final i0.GeneratedColumn<BigInt> sourceTableId =
      i0.GeneratedColumn<BigInt>('source_table_id', aliasedName, true,
          generatedAs:
              i0.GeneratedAs(sourceDynamicColumns.dynamicTableId, false),
          type: i0.DriftSqlType.bigInt);
  late final i0.GeneratedColumn<BigInt> targetColumnId =
      i0.GeneratedColumn<BigInt>('target_column_id', aliasedName, true,
          generatedAs: i0.GeneratedAs(targetDynamicColumns.id, false),
          type: i0.DriftSqlType.bigInt);
  late final i0.GeneratedColumn<BigInt> targetTableId =
      i0.GeneratedColumn<BigInt>('target_table_id', aliasedName, true,
          generatedAs:
              i0.GeneratedAs(targetDynamicColumns.dynamicTableId, false),
          type: i0.DriftSqlType.bigInt);
  late final i0.GeneratedColumn<BigInt> foreignKeyId =
      i0.GeneratedColumn<BigInt>('foreign_key_id', aliasedName, true,
          generatedAs: i0.GeneratedAs(foreignKeys.id, false),
          type: i0.DriftSqlType.bigInt);
  late final i0.GeneratedColumn<BigInt> relationId = i0.GeneratedColumn<BigInt>(
      'relation_id', aliasedName, true,
      generatedAs: i0.GeneratedAs(foreignKeys.relationId, false),
      type: i0.DriftSqlType.bigInt);
  @override
  $DynamicColumnsWithForeignKeysView createAlias(String alias) {
    return $DynamicColumnsWithForeignKeysView(attachedDatabase, alias);
  }

  @override
  i0.Query? get query =>
      (attachedDatabase.selectOnly(sourceDynamicColumns)..addColumns($columns))
          .join([
        i2.leftOuterJoin(foreignKeys,
            foreignKeys.sourcePropertyId.equalsExp(sourceDynamicColumns.id)),
        i2.leftOuterJoin(targetDynamicColumns,
            targetDynamicColumns.id.equalsExp(foreignKeys.targetPropertyId))
      ]);
  @override
  Set<String> get readTables =>
      const {'dynamic_columns', 'dynamic_table_foreign_keys'};
}

from drift.

AlexandreAndrade00 avatar AlexandreAndrade00 commented on August 16, 2024

I left comments in the generated code where the tables are not available.

from drift.

AlexandreAndrade00 avatar AlexandreAndrade00 commented on August 16, 2024

The tables are not available in the attachedDatabase property and the imports are not being created too, misses the import 'package:persistence/src/database/models/dynamic_database/dynamic_database.drift.dart';.

from drift.

AlexandreAndrade00 avatar AlexandreAndrade00 commented on August 16, 2024

Thank you! :D

from drift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.