serverpod / serverpod_docs

Serverpod's official documentation.
21 stars 61 forks source link

`db` getter for model does not get created following the get started guide #196

Open dshukertjr opened 1 month ago

dshukertjr commented 1 month ago

I'm going through the get started guide and am getting stuck at the Object database mapping section.

The db related code seems to not be generated, and I am getting The getter 'db' isn't defined for the type 'Company'. error.

Steps to reproduce

  1. run serverpod create mypod
  2. cd into the server and run it and confirm it's running properly
    cd mypod/mypod_server
    docker compose up --build --detach
    dart bin/main.dart --apply-migrations
  3. Kill the terminal with control + c
  4. Create a mypod_server/lib/src/models/company.spy.yaml file with the following:
    class: Company
    table: company
    fields:
      name: String
      foundedDate: DateTime?
  5. Run serverpod generate inside mypod_server
  6. Run serverpod create-migration inside mypod_server
  7. Run the server with dart bin/main.dart --apply-migrations
  8. Add the following into the mypod_flutter app and observe the error.
    await Company.db.insertRow(session, myCompany);
    Screenshot 2024-10-28 at 21 19 18

Here is what I have for mypod_client/lib/src/protocol/company.dart

/* AUTOMATICALLY GENERATED CODE DO NOT MODIFY */
/*   To generate run: "serverpod generate"    */

// ignore_for_file: implementation_imports
// ignore_for_file: library_private_types_in_public_api
// ignore_for_file: non_constant_identifier_names
// ignore_for_file: public_member_api_docs
// ignore_for_file: type_literal_in_constant_pattern
// ignore_for_file: use_super_parameters

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:serverpod_client/serverpod_client.dart' as _i1;

abstract class Company implements _i1.SerializableModel {
  Company._({
    this.id,
    required this.name,
    this.foundedDate,
  });

  factory Company({
    int? id,
    required String name,
    DateTime? foundedDate,
  }) = _CompanyImpl;

  factory Company.fromJson(Map<String, dynamic> jsonSerialization) {
    return Company(
      id: jsonSerialization['id'] as int?,
      name: jsonSerialization['name'] as String,
      foundedDate: jsonSerialization['foundedDate'] == null
          ? null
          : _i1.DateTimeJsonExtension.fromJson(
              jsonSerialization['foundedDate']),
    );
  }

  /// The database id, set if the object has been inserted into the
  /// database or if it has been fetched from the database. Otherwise,
  /// the id will be null.
  int? id;

  String name;

  DateTime? foundedDate;

  Company copyWith({
    int? id,
    String? name,
    DateTime? foundedDate,
  });
  @override
  Map<String, dynamic> toJson() {
    return {
      if (id != null) 'id': id,
      'name': name,
      if (foundedDate != null) 'foundedDate': foundedDate?.toJson(),
    };
  }

  @override
  String toString() {
    return _i1.SerializationManager.encode(this);
  }
}

class _Undefined {}

class _CompanyImpl extends Company {
  _CompanyImpl({
    int? id,
    required String name,
    DateTime? foundedDate,
  }) : super._(
          id: id,
          name: name,
          foundedDate: foundedDate,
        );

  @override
  Company copyWith({
    Object? id = _Undefined,
    String? name,
    Object? foundedDate = _Undefined,
  }) {
    return Company(
      id: id is int? ? id : this.id,
      name: name ?? this.name,
      foundedDate: foundedDate is DateTime? ? foundedDate : this.foundedDate,
    );
  }
}