This file is indexed.

/usr/share/perl5/Rose/DB/Object/Metadata/Auto.pm is in librose-db-object-perl 1:0.815-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
package Rose::DB::Object::Metadata::Auto;

use strict;

use Carp();

use Rose::DB::Object::Metadata::Column::Scalar;
use Rose::DB::Object::Metadata::ForeignKey;

use Rose::DB::Object::Metadata;
our @ISA = qw(Rose::DB::Object::Metadata);

our $Debug;

*Debug = \$Rose::DB::Object::Metadata::Debug;

our $Sort_Columns_Alphabetically = 0; # hack for test suite

our $VERSION = '0.786';

our $Missing_PK_OK = 0;

use Rose::Class::MakeMethods::Generic
(
  inheritable_scalar => 
  [
    'default_perl_indent',
    'default_perl_braces',
    'default_perl_unique_key_style',
  ],

  inheritable_hash =>
  [
    relationship_type_ranks => { interface => 'get_set_all' },
    relationship_type_rank   => { interface => 'get_set', hash_key => 'relationship_type_ranks' },
    delete_relationship_type_rank => { interface => 'delete', hash_key => 'relationship_type_ranks' },
  ],
);

use Rose::Object::MakeMethods::Generic
(
  'scalar --get_set_init' => 
  [
    'column_alias_generator',
    'foreign_key_name_generator',
  ],

  scalar => 'auto_init_args',
);

__PACKAGE__->relationship_type_ranks
(
  'one to one'   => 1,
  'many to one'  => 2,
  'one to many'  => 3,
  'many to many' => 4,
);

__PACKAGE__->default_perl_indent(4);
__PACKAGE__->default_perl_braces('k&r');
__PACKAGE__->default_perl_unique_key_style('array');

sub auto_formatted_schema
{
  my($self, $db) = @_;

  $db ||= $self->db;

  my $schema = $self->select_schema($db);

  $schema = $db->default_implicit_schema  unless(defined $schema);

  if(defined $schema)
  {
    if($db->likes_lowercase_schema_names)
    {
      $schema = lc $schema;
    }
    elsif($db->likes_uppercase_schema_names)
    {
      $schema = uc $schema;
    }
  }

  return $schema;
}

sub auto_formatted_catalog
{
  my($self, $db) = @_;

  $db ||= $self->db;

  my $catalog = $self->select_catalog($db);

  if(defined $catalog)
  {
    if($db->likes_lowercase_catalog_names)
    {
      $catalog = lc $catalog;
    }
    elsif($db->likes_uppercase_catalog_names)
    {
      $catalog = uc $catalog;
    }
  }

  return $catalog;
}

sub auto_generate_columns
{
  my($self) = shift;

  my($db, $class, %columns, $catalog, $schema, $table, $error);

  TRY:
  {
    local $@;

    eval
    {
      $class = $self->class or die "Missing class!";

      $db = $self->db;
      my $dbh = $db->dbh or die $db->error;

      local $dbh->{'FetchHashKeyName'} = 'NAME';

      $table = $self->table;

      $table = lc $table  if($db->likes_lowercase_table_names);

      my $table_unquoted = $db->unquote_table_name($table);

      $catalog = $self->auto_formatted_catalog($db);
      $schema  = $self->auto_formatted_schema($db);

      my $sth = $dbh->column_info($catalog, $schema, $table_unquoted, '%');

      unless(defined $sth)
      {
        no warnings; # undef strings okay
        die "No column information found for catalog '", $catalog,
            "' schema '", $schema, "' table '", $table_unquoted, "'";
      }

      COLUMN: while(my $col_info = $sth->fetchrow_hashref)
      {
        CHECK_TABLE: # Make sure this column is from the right table
        {
          no warnings; # Allow undef coercion to empty string

          $col_info->{'TABLE_NAME'} = $db->unquote_table_name($col_info->{'TABLE_NAME'});

          next COLUMN unless($col_info->{'TABLE_CAT'}   eq $catalog &&
                             $col_info->{'TABLE_SCHEM'} eq $schema &&
                             $col_info->{'TABLE_NAME'}  eq $table_unquoted);
        }

        unless(defined $col_info->{'COLUMN_NAME'})
        {
          Carp::croak "Could not extract column name from DBI column_info()";
        }

        $db->refine_dbi_column_info($col_info, $self);

        $columns{$col_info->{'COLUMN_NAME'}} = 
          $self->auto_generate_column($col_info->{'COLUMN_NAME'}, $col_info);
      }
    };

    $error = $@;
  }

  if($error || !keys %columns)
  {
    no warnings; # undef strings okay
    Carp::croak "Could not auto-generate columns for class $class - ",
                ($error || "no column info found for catalog '" . $catalog .
                "' schema '" . $schema . "' table '$table'");
  }

  $self->auto_alias_columns(values %columns);

  return wantarray ? values %columns : \%columns;
}

sub auto_alias_columns
{
  my($self) = shift;

  foreach my $column (@_) #(@_ == 1 && ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_)
  {
    # Auto-alias the column if there will be any conflicts
    foreach my $type ($column->auto_method_types)
    {
      my $method = $self->method_name_from_column($column, $type);

      if($self->method_name_is_reserved($method, $self->class))
      {
        $self->auto_alias_column($column);
        last; # just alias the column once
      }
    }

    # Re-check: errors are fatal this time
    foreach my $type ($column->auto_method_types)
    {
      my $method = $self->method_name_from_column($column, $type);

      if($self->method_name_is_reserved($method, $self->class))
      {
        Carp::croak "Cannot create '$type' method named '$method' for ",
                    "column '$column' - method name is reserved";
      }
    }
  }
}

sub auto_generate_column
{
  my($self, $name, $col_info) = @_;

  my $type = $col_info->{'TYPE_NAME'};
  my $meta_class = $self->original_class;

  my $column_class = 
    $meta_class->column_type_class($type) || $meta_class->column_type_class('scalar')
      or Carp::croak "No column class set for column types '$type' or 'scalar'";

  unless($self->column_class_is_loaded($column_class))
  {
    $self->load_column_class($column_class);
  }

  my $column = $column_class->new(name => $name, parent => $self);

  $column->init_with_dbi_column_info($col_info);

  return $column;
}

sub init_column_alias_generator { sub { $_[1] . '_col' } }

DEFAULT_FK_NAME_GEN:
{
  my %Seen_FK_Name;

  sub default_foreign_key_name_generator
  {
    my($meta, $fk) = @_;

    my $class       = $meta->class;
    my $key_columns = $fk->key_columns;

    my $name = $fk->name;

    # No single column whose name we can steal and then
    # mangle to make the foreign key name, so we'll derive
    # the foreign key name from the foreign class name.
    if(keys %$key_columns > 1)
    {
      $name = $fk->class;
      $name =~ s/::/_/g;
      $name =~ s/([a-z])([A-Z])/$1_$2/g;
      $name = lc $name;
    }
    else
    {
      my($local_column, $foreign_column) = %$key_columns;

      # Try to lop off foreign column name.  Example:
      # my_foreign_object_id -> my_foreign_object
      if($local_column =~ s/_$foreign_column$//)
      {
        $name = $local_column;
      }
      else
      {
        # Usually, the actual column name is taken by the column accessor,
        # but if it's not, we'll use it.
        if(!$meta->class->can($local_column))
        {
          $name = $local_column;
        }
        else # otherwise, append "_object"
        {
          $name = $local_column . '_object';
        }
      }
    }

    # Make sure the name's not taken, appending numbers until it's unique.
    # See, this is why you shouldn't rely on auto_init_* all the time.
    # You end up with lame method names.
    if($Seen_FK_Name{$class}{$name})
    {
      my $num = 2;
      my $new_name = $name;

      while($Seen_FK_Name{$class}{$new_name})
      {
        $new_name = $name . $num++;
      }

      $name = $new_name;
    }

    $Seen_FK_Name{$class}{$name}++;

    return $name;
  }
}

sub init_foreign_key_name_generator { \&default_foreign_key_name_generator }

sub auto_alias_column
{
  my($self, $column) = @_;

  my $code = $self->column_alias_generator;
  local $_ = $column->name;

  my $alias = $code->($self, $_);

  if($self->method_name_is_reserved($alias, $self->class))
  {
    Carp::croak "Called column_alias_generator() to alias column ",
                "'$_' but the value returned is a reserved method ",
                "name: $alias";
  }

  $column->alias($alias);

  return;
}

sub auto_retrieve_primary_key_column_names
{
  my($self) = shift;

  unless(defined wantarray)
  {
    Carp::croak "Useless call to auto_retrieve_primary_key_column_names() in void context";
  }

  my $db      = $self->db;
  my $catalog = $self->auto_formatted_catalog($db);
  my $schema  = $self->auto_formatted_schema($db);

  my($pk_columns, $error);

  TRY:
  {
    local $@;

    eval
    {
      $pk_columns = 
        $self->db->primary_key_column_names(table   => $self->table,
                                            catalog => $catalog,
                                            schema  => $schema);
    };

    $error = $@;
  }

  if($error || (!$Missing_PK_OK && !@$pk_columns))
  {
    $error = 'no primary key columns found'  unless(defined $error);
    Carp::croak "Could not auto-retrieve primary key columns for class ",
                $self->class, " - ",
                ($error || "no primary key info found for catalog '" . $catalog .
                "' schema '" . $schema . "' table '" . $self->table, "'");
  }

  $pk_columns ||= [];

  return wantarray ? @$pk_columns : $pk_columns;
}

my %Warned;

sub auto_generate_foreign_keys
{
  my($self, %args) = @_;

  unless(defined wantarray)
  {
    Carp::croak "Useless call to auto_generate_foreign_keys() in void context";
  }

  my $no_warnings = $args{'no_warnings'};

  my($class, @foreign_keys, $total_fks, %used_names, $error);

  TRY:
  {
    local $@;

    eval
    {
      $class = $self->class or die "Missing class!";

      my $db  = $self->db;
      my $dbh = $db->dbh or die $db->error;

      local $dbh->{'FetchHashKeyName'} = 'NAME';

      my $catalog = $self->auto_formatted_catalog($db);
      my $schema  = $self->auto_formatted_schema($db);

      my $table = $db->likes_lowercase_table_names ? lc $self->table : $self->table;

      my $sth = $dbh->foreign_key_info(undef, undef, undef,
                                       $catalog, $schema, $table);

      # This happens when the table has no foreign keys
      return  unless(defined $sth);

      my(%fk, @fk_info);

      FK: while(my $fk_info = $sth->fetchrow_hashref)
      {
        $db->refine_dbi_foreign_key_info($fk_info, $self);

        CHECK_TABLE: # Make sure this column is from the right table
        {
          no warnings; # Allow undef coercion to empty string
          next FK  unless($fk_info->{'FK_TABLE_CAT'}   eq $catalog &&
                          $fk_info->{'FK_TABLE_SCHEM'} eq $schema &&
                          $fk_info->{'FK_TABLE_NAME'}  eq $table);
        }

        my $local_column   = $fk_info->{'FK_COLUMN_NAME'};
        my $foreign_column = $fk_info->{'UK_COLUMN_NAME'};

        my $fk_id = $fk_info->{'RDBO_FK_ID'} = $fk_info->{'FK_NAME'} || $fk_info->{'UK_NAME'};

        $fk{$fk_id}{'key_columns'}{$local_column} = $foreign_column;

        push(@fk_info, $fk_info);
      }

      # This step is important!  It ensures that foreign keys will be created
      # in a deterministic order, which in turn allows the "auto-naming" of
      # foreign keys to work in a predictable manner.  This exact sort order
      # (lowercase table name comparisons) is part of the API for foreign
      # key auto generation.
      @fk_info = 
        sort { lc $a->{'UK_TABLE_NAME'} cmp lc $b->{'UK_TABLE_NAME'} } @fk_info;

      my $cm = $self->convention_manager;

      my %seen_fk_id;

      FK_INFO: foreach my $fk_info (@fk_info)
      {
        my $fk_id = $fk_info->{'RDBO_FK_ID'};

        next  if($seen_fk_id{$fk_id}++);

        my $foreign_class = 
          $self->class_for(catalog => $fk_info->{'UK_TABLE_CAT'},
                           schema  => $fk_info->{'UK_TABLE_SCHEM'},
                           table   => $fk_info->{'UK_TABLE_NAME'});

        unless($foreign_class) # Give convention manager a chance
        {
          $foreign_class = 
            $self->convention_manager->related_table_to_class(
              $fk_info->{'UK_TABLE_NAME'}, $self->class);

          unless(UNIVERSAL::isa($foreign_class, 'Rose::DB::Object'))
          {
            # Null convention manager may return undef
            no warnings 'uninitialized'; 
            eval "require $foreign_class";
            $foreign_class = undef  if($@ || !UNIVERSAL::isa($foreign_class, 'Rose::DB::Object'));
          }
        }

        unless($foreign_class)
        {
          my $key = join($;, map { defined($_) ? $_ : "\034" } $self->class,
                         @$fk_info{qw(UK_TABLE_CAT UK_TABLE_SCHEM UK_TABLE_NAME)});

          # Add deferred task
          $self->add_deferred_task(
          {
            class  => $self->class, 
            method => 'auto_init_foreign_keys',
            args   => \%args,

            code => sub
            {
              $self->auto_init_foreign_keys(%args);
              $self->make_foreign_key_methods(%args, preserve_existing => 1);
            },

            check => sub
            {
              my $fks = $self->foreign_keys;
              return @$fks == $total_fks ? 1 : 0;
            },
          });

          unless($no_warnings || $Warned{$key}++ || $self->allow_auto_initialization)
          {
            no warnings; # Allow undef coercion to empty string
            Carp::carp
              "No Rose::DB::Object-derived class found for catalog '",
              $fk_info->{'UK_TABLE_CAT'}, "' schema '", 
              $fk_info->{'UK_TABLE_SCHEM'}, "' table '", 
              $fk_info->{'UK_TABLE_NAME'}, "'";
          }

          $total_fks++;
          next FK_INFO;
        }

        $fk{$fk_id}{'class'} = $foreign_class;

        my $key_name =
          $cm->auto_foreign_key_name($foreign_class, $fk_id, 
                                     $fk{$fk_id}{'key_columns'},
                                     \%used_names);

        $used_names{$key_name}++  if(defined $key_name);

        if(defined $key_name && length $key_name)
        {
          $fk{$fk_id}{'name'} = $key_name;
        }

        $total_fks++;
      }

      my(%seen, %seen_name);

      foreach my $fk_info (@fk_info)
      {
        next  if($seen{$fk_info->{'RDBO_FK_ID'}}++);
        my $info = $fk{$fk_info->{'RDBO_FK_ID'}};
        my $fk   = Rose::DB::Object::Metadata::ForeignKey->new(%$info);

        next  unless(defined $fk->class);

        unless(defined $fk->name)
        {
          $fk->name($self->foreign_key_name_generator->($self, $fk));
        }

        push(@foreign_keys, $fk);
      }
    };

    $error = $@;
  }

  if($error)
  {
    Carp::croak "Could not auto-generate foreign keys for class $class - $error";
  }

  @foreign_keys = sort { lc $a->name cmp lc $b->name } @foreign_keys;

  return wantarray ? @foreign_keys : \@foreign_keys;
}

sub auto_init_columns
{
  my($self, %args) = @_;

  my $auto_columns     = $self->auto_generate_columns;
  my $existing_columns = $self->columns;

  if(!$args{'replace_existing'} && keys %$auto_columns != @$existing_columns)
  {
    while(my($name, $column) = each(%$auto_columns))
    {
      next  if($self->column($name));
      $self->add_column($column);
    }
  }
  elsif($args{'replace_existing'} || !@$existing_columns)
  {
    $self->columns(values %$auto_columns);
  }

  return;
}

sub perl_columns_definition
{
  my($self, %args) = @_;

  my $for_setup = $args{'for_setup'};
  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
  my $braces = defined $args{'braces'} ? $args{'braces'} : $self->default_perl_braces;

  unless($indent =~ /^\d+$/)
  {
    Carp::croak 'Invalid ', (defined $args{'indent'} ? '' : 'default '),
                "indent size: '$braces'";
  }

  $indent = ' ' x $indent;

  my $def_start = $for_setup ? 'columns => ' : "__PACKAGE__->meta->columns";

  my $ob = $for_setup ? '[' : '(';
  my $cb = $for_setup ? ']' : ')';

  if($braces eq 'bsd')
  {
    $def_start .= "\n$ob\n";
  }
  elsif($braces eq 'k&r')
  {
    $def_start .= "$ob\n";
  }
  else
  {
    Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '),
                "brace style: '$braces'";
  }

  my $max_len = 0;
  my $min_len = -1;

  foreach my $name ($self->column_names)
  {
    $max_len = length($name)  if(length $name > $max_len);
    $min_len = length($name)  if(length $name < $min_len || $min_len < 0);
  }

  my @col_defs;

  no warnings 'uninitialized'; # ordinal_position may be undef
  foreach my $column (sort __by_rank $self->columns)
  {
    push(@col_defs, $column->perl_hash_definition(inline       => 1, 
                                                  name_padding => $max_len));
  }

  $cb = $for_setup ? '],' : ');';

  my $perl = $def_start . join(",\n", map { "$indent$_" } @col_defs) . ",\n$cb\n";

  if($for_setup)
  {
    for($perl)
    {
      s/^/$indent/mg;
      s/\n\z//;
      s/^[ \t]+$//mg;
    }
  }

  return $perl;
}

sub __by_rank
{  
  my $pos1 = $a->ordinal_position;
  my $pos2 = $b->ordinal_position;

  if(!$Sort_Columns_Alphabetically && defined $pos1 && defined $pos2)
  {
    return $pos1 <=> $pos2 || lc($a->name) cmp lc($b->name);
  }

  return lc($a->name) cmp lc($b->name);
}

sub perl_foreign_keys_definition
{
  my($self, %args) = @_;

  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
  my $braces = defined $args{'braces'} ? $args{'braces'} : $self->default_perl_braces;

  unless($indent =~ /^\d+$/)
  {
    Carp::croak 'Invalid ', (defined $args{'indent'} ? '' : 'default '),
                "indent size: '$braces'";
  }

  my $indent_txt = ' ' x $indent;

  my $for_setup = $args{'for_setup'};

  my $def = $for_setup ? 
    $indent_txt . 'foreign_keys => ' : 
    '__PACKAGE__->meta->foreign_keys';

  my $ob = $for_setup ? '[' : '(';
  my $cb = $for_setup ? ']' : ')';

  if($braces eq 'bsd')
  {
    $def .= "\n$ob\n";
  }
  elsif($braces eq 'k&r')
  {
    $def .= "$ob\n";
  }
  else
  {
    Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '),
                "brace style: '$braces'";
  }

  my @fk_defs;

  foreach my $fk ($self->foreign_keys)
  {
    push(@fk_defs, $fk->perl_hash_definition(indent => $indent, braces => $braces));
  }

  return ''  unless(@fk_defs);

  foreach my $fk_def (@fk_defs)
  {
    for($fk_def)
    {
      s/^/$indent_txt/mg;
      s/^[ \t]+$//mg;
    }

    $def .= "$fk_def,\n" . ($fk_def eq $fk_defs[-1] ? '' : "\n");
  }

  if($for_setup)
  {
    $def .= "],\n";

    for($def)
    {
      s/^/$indent_txt/mg;
      s/\n\z//;
      s/\A$indent_txt//;
      s/^[ \t]+$//mg;
    }

    return $def;
  }
  else
  {
    return $def . ");\n";
  }
}

sub perl_relationships_definition
{
  my($self, %args) = @_;

  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
  my $braces = defined $args{'braces'} ? $args{'braces'} : $self->default_perl_braces;

  unless($indent =~ /^\d+$/)
  {
    Carp::croak 'Invalid ', (defined $args{'indent'} ? '' : 'default '),
                "indent size: '$braces'";
  }

  my $indent_txt = ' ' x $indent;

  my $for_setup = $args{'for_setup'};

  my $def = $for_setup ? 
    $indent_txt . 'relationships => ' : 
    '__PACKAGE__->meta->relationships';

  my $ob = $for_setup ? '[' : '(';
  my $cb = $for_setup ? ']' : ')';

  if($braces eq 'bsd')
  {
    $def .= "\n$ob\n";
  }
  elsif($braces eq 'k&r')
  {
    $def .= "$ob\n";
  }
  else
  {
    Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '),
                "brace style: '$braces'";
  }

  my @rel_defs;

  foreach my $rel ($self->relationships)
  {
    next  if($rel->can('foreign_key') && $rel->foreign_key);
    push(@rel_defs, $rel->perl_hash_definition(indent => $indent, braces => $braces));
  }

  return ''  unless(@rel_defs);

  foreach my $rel_def (@rel_defs)
  {
    for($rel_def)
    {
      s/^/$indent_txt/mg;
      s/^[ \t]+$//mg;
    }

    $def .= "$rel_def,\n" . ($rel_def eq $rel_defs[-1] ? '' : "\n");
  }

  if($for_setup)
  {
    $def .= "],\n";

    for($def)
    {
      s/^/$indent_txt/mg;
      s/\n\z//;
      s/\A$indent_txt//;
      s/^[ \t]+$//mg;
    }

    return $def;
  }
  else
  {
    return $def . ");\n";
  }
}

sub perl_unique_keys_definition
{
  my($self, %args) = @_;

  my $style  = defined $args{'style'}  ? $args{'style'}  : $self->default_perl_unique_key_style;
  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
  my $braces = defined $args{'braces'} ? $args{'braces'} : $self->default_perl_braces;

  unless($indent =~ /^\d+$/)
  {
    Carp::croak 'Invalid ', (defined $args{'indent'} ? '' : 'default '),
                "indent size: '$braces'";
  }

  $indent = ' ' x $indent;

  my $uk_perl_method;

  if($style eq 'array')
  {
    $uk_perl_method = 'perl_array_definition';
  }
  elsif($style eq 'object')
  {
    $uk_perl_method = 'perl_object_definition';
  }
  else
  {
    Carp::croak 'Invalid ', (defined $args{'style'} ? '' : 'default '),
                "unique key style: '$style'";
  }

  my @uk_defs;

  foreach my $uk ($self->unique_keys)
  {
    push(@uk_defs, $uk->$uk_perl_method());
  }

  return ''  unless(@uk_defs);

  my $for_setup = $args{'for_setup'};

  my $def_start = $for_setup ? 
    $indent . 'unique_keys => ' : 
    '__PACKAGE__->meta->unique_keys';

  my $ob = $for_setup ? '[' : '(';
  my $cb = $for_setup ? ']' : ')';

  if(@uk_defs == 1)
  {
    $def_start .= $ob;
  }
  elsif($braces eq 'bsd')
  {
    $def_start .= "\n$ob\n";
  }
  elsif($braces eq 'k&r')
  {
    $def_start .= "$ob\n";
  }
  else
  {
    Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '),
                "brace style: '$braces'";
  }

  if($for_setup)
  {
    if(@uk_defs == 1)
    {
      $def_start =~ s/^(\s*)unique_keys/$1unique_key/;

      for($uk_defs[0])
      {
        s/\A\[ //;
        s/ \]\z//;
      }

      return "$def_start $uk_defs[0] ],";
    }
    else
    {
      my $perl = $def_start . join(",\n", map { "$indent$_" } @uk_defs) . ",\n],";

      for($perl)
      {
        s/^/$indent/mg;
        s/\A$indent//;
        s/^[ \t]+$//mg;
      }

      return $perl;
    }
  }
  else
  {
    if(@uk_defs == 1)
    {
      return "$def_start$uk_defs[0]);\n";
    }
    else
    {
      return $def_start . join(",\n", map { "$indent$_" } @uk_defs) . ",\n);\n";
    }
  }
}

sub perl_metadata_attributes
{
  my($self, %args) = @_;

  my $for_setup = $args{'for_setup'};
  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;

  $indent = $for_setup ? (' ' x $indent) : '';

  my @attrs;

  foreach my $attr (qw(allow_inline_column_values))
  {
    no strict 'refs';
    if(my $value = $self->$attr())
    {
      if($for_setup)
      {
        push(@attrs, "$attr => " . $self->perl_quote_value($value) . ',');
      }
      else
      {
        push(@attrs, "__PACKAGE__->meta->$attr(" . $self->perl_quote_value($value) . ');');
      }
    }
  }

  return @attrs ? (join("\n", map { "$indent$_" } @attrs) . ($for_setup ? '' : "\n")) : '';
}

sub perl_quote_value
{
  my($self, $value) = @_;

  return $value  if($value =~ /^\d+$/);

  for($value)
  {
    s/\\/\\\\/g;
    s/'/\\'/g;
  }

  return qq('$value');
}
sub perl_table_definition
{
  my($self, %args) = @_;

  my $for_setup = $args{'for_setup'};
  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;

  my $table = $self->table;
  $table =~ s/'/\\'/;

  if($args{'for_setup'})
  {
    $indent = ' ' x $indent;
    return qq(${indent}table   => '$table',);
  }

  return qq(__PACKAGE__->meta->table('$table'););  
}

sub perl_primary_key_columns_definition
{
  my($self, %args) = @_;

  my @pk_cols = $self->primary_key->column_names;

  Carp::croak "No primary key columns found for class ", ref($self)
    unless(@pk_cols);

  if($args{'for_setup'})
  {
    my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
    $indent = ' ' x $indent;
    return $indent . 'primary_key_columns => ' . 
           $self->primary_key->perl_array_definition . ',';
  }
  else
  {
    return '__PACKAGE__->meta->primary_key_columns(' .
           $self->primary_key->perl_array_definition . ");\n";
  }
}

sub perl_class_definition
{
  my($self, %args) = @_;

  my $indent = defined $args{'indent'} ? $args{'indent'} : $self->default_perl_indent;
  my $braces = defined $args{'braces'} ? $args{'braces'} : $self->default_perl_braces;

  my $class = $self->class;

  no strict 'refs';
  my $isa = delete $args{'isa'} || [ ${"${class}::ISA"}[0] || 'Rose::DB::Object' ];

  $isa = [ $isa ]  unless(ref $isa);

  my %use;

  foreach my $fk ($self->foreign_keys)
  {
    $use{$fk->class}++;
  }

  foreach my $rel ($self->relationships)
  {
    if($rel->can('map_class'))
    {
      $use{$rel->map_class}++;
    }
    else
    {
      $use{$rel->class}++;
    }
  }

  my $foreign_modules = '';

  if(%use && !$self->auto_load_related_classes)
  {
    $foreign_modules = "\n\n" . join("\n", map { "use $_;"} sort keys %use);
  }

  if(defined $args{'use_setup'} && !$args{'use_setup'})
  {
    return<<"EOF";
package $class;

use strict;

use base qw(@$isa);$foreign_modules

@{[ $self->perl_table_definition(%args) ]}

@{[join("\n", grep { /\S/ } $self->perl_columns_definition(%args),
                            $self->perl_primary_key_columns_definition(%args),
                            $self->perl_unique_keys_definition(%args),
                            $self->perl_metadata_attributes(%args),
                            $self->perl_foreign_keys_definition(%args),
                            $self->perl_relationships_definition(%args))]}
__PACKAGE__->meta->initialize;

1;
EOF
  }
  else
  {
    my $setup_start = '__PACKAGE__->meta->setup';

    if($braces eq 'bsd')
    {
      $setup_start .= "\n(";
    }
    elsif($braces eq 'k&r')
    {
      $setup_start .= "(";
    }
    else
    {
      Carp::croak 'Invalid ', (defined $args{'braces'} ? '' : 'default '),
                  "brace style: '$braces'";
    }

    $args{'for_setup'} = 1;
    $indent = ' ' x $indent;

    return<<"EOF";
package $class;

use strict;

use base qw(@$isa);$foreign_modules

$setup_start
@{[join("\n\n", grep { /\S/ } $self->perl_table_definition(%args),
                              $self->perl_columns_definition(%args),
                              $self->perl_primary_key_columns_definition(%args),
                              $self->perl_unique_keys_definition(%args),
                              $self->perl_metadata_attributes(%args),
                              $self->perl_foreign_keys_definition(%args),
                              $self->perl_relationships_definition(%args))]}
);

1;
EOF
  }
}

sub auto_generate_unique_keys { die "Override in subclass" }

sub auto_init_unique_keys
{
  my($self, %args) = @_;

  return  if(exists $args{'with_unique_keys'} && !$args{'with_unique_keys'});

  my $pk_cols = join("\0", $self->primary_key_columns);

  unless(length $pk_cols)
  {
    $pk_cols = join("\0", $self->auto_retrieve_primary_key_column_names);
  }

  my $auto_unique_keys     = $self->auto_generate_unique_keys;
  my $existing_unique_keys = $self->unique_keys;

  if(!$args{'replace_existing'} && @$auto_unique_keys != @$existing_unique_keys)
  {
    KEY: foreach my $key (@$auto_unique_keys)
    {
      my $id = join("\0", sort map { lc } $key->column_names);

      foreach my $existing_key (@$existing_unique_keys)
      {
        next KEY  if($id eq join("\0", sort map { lc } $existing_key->column_names));
      }

      # Skip primary key
      next KEY  if($pk_cols eq join("\0", $key->column_names));

      $self->add_unique_key($key);
    }
  }
  elsif($args{'replace_existing'} || !@$existing_unique_keys)
  {
    $self->unique_keys(@$auto_unique_keys);
  }

  return;
}

sub auto_init_foreign_keys
{
  my($self, %args) = @_;

  if(exists $args{'with_foreign_keys'} && !$args{'with_foreign_keys'})
  {
    $self->initialized_foreign_keys(1);
    return;
  }

  my $auto_foreign_keys     = $self->auto_generate_foreign_keys(%args);
  my $existing_foreign_keys = $self->foreign_keys;

  if(!$args{'replace_existing'} && @$auto_foreign_keys != @$existing_foreign_keys)
  {
    KEY: foreach my $key (@$auto_foreign_keys)
    {
      my $id = __fk_key_to_id($key); # $key->id; # might not have parent yet

      foreach my $existing_key (@$existing_foreign_keys)
      {
        next KEY  if($id eq __fk_key_to_id($existing_key)); # $existing_key->id
      }

      $self->add_foreign_key($key);
    }
  }
  elsif($args{'replace_existing'} || !@$existing_foreign_keys)
  {
    $self->foreign_keys(@$auto_foreign_keys);
  }

  $self->initialized_foreign_keys(1);

  return;
}

sub __fk_key_to_id
{
  my($fk) = shift;

  my $key_columns = $fk->key_columns;

  return
    join("\0", map { join("\1", $_, $key_columns->{$_}) } sort keys %$key_columns);
}

sub auto_init_primary_key_columns
{
  my($self) = shift;

  my $primary_key_columns = $self->auto_retrieve_primary_key_column_names;

  unless($primary_key_columns && @$primary_key_columns)
  {
    if($Missing_PK_OK)
    {
      $primary_key_columns = [];
    }
    else
    {
      Carp::croak "Could not retrieve primary key columns for class ", ref($self);
    }
  }

  $self->primary_key_columns(@$primary_key_columns);

  return;
}

my %Auto_Rel_Types;

sub auto_init_relationships
{
  my($self) = shift;
  my(%args) = @_;

  my $type_map  = $self->relationship_type_classes;
  my @all_types = keys %$type_map;

  my %types;

  if(delete $args{'restore_types'})
  {
    if(my $types = $Auto_Rel_Types{$self->class})
    {
      $args{'types'} = $types;
    }
  }

  if(exists $args{'relationship_types'} || 
     exists $args{'types'} || 
     exists $args{'with_relationships'})
  {
    my $types = exists $args{'relationship_types'} ? 
                delete $args{'relationship_types'} :
                exists $args{'types'} ?
                delete $args{'types'} :
                exists $args{'with_relationships'} ?
                delete $args{'with_relationships'} : 1;

    if(ref $types)
    {
      %types = map { $_ => 1 } @$types;
      $Auto_Rel_Types{$self->class} = $types;
    }
    elsif($types)
    {
      %types = map { $_ => 1 } @all_types;
    }
    else
    {
      $Auto_Rel_Types{$self->class} = [];
    }
  }
  else
  {
    %types = map { $_ => 1 } @all_types;
  }

  if(delete $args{'replace_existing'})
  {
    foreach my $rel ($self->relationships)
    {
      next  unless($types{$rel->type});
      $self->delete_relationship($rel->name);
    }
  }

  foreach my $type (sort { $self->sort_relationship_types($a, $b) } keys %types)
  {
    my $type_name = $type;

    for($type_name)
    {
      s/ /_/g;
      s/\W+//g;
    }

    my $method = 'auto_init_' . $type_name . '_relationships';

    if($self->can($method))
    {
      $self->$method(@_);
    }
  }

  return;
}

sub sort_relationship_types
{
  my($self, $a, $b) = @_;
  return $self->relationship_type_rank($a) <=> $self->relationship_type_rank($b);
}

sub auto_init_one_to_one_relationships
{
  my($self, %args) = @_;

  my $class = $self->class;

  # For each foreign key in this class, try to make a "one to one"
  # relationship in the table that the foreign key points to.  But
  # don't do so unless the keys on both sides of the relationship
  # are unique or primary keys.
  FK: foreach my $fk ($self->foreign_keys)
  {
    my $f_class = $fk->class;

    next  unless($f_class && UNIVERSAL::isa($f_class, 'Rose::DB::Object'));

    my $f_meta  = $f_class->meta;
    my $key_cols = $fk->key_columns;

    # If both sides of the column map are unique or primary keys, then
    # this is really a one-to-one relationship
    my $local_key  = join("\0", sort keys %$key_cols);
    my $remote_key = join("\0", sort values %$key_cols);

    my($local_unique, $remote_unique);

    my $local_meta = $class->meta;

    foreach my $uk ($local_meta->primary_key, $local_meta->unique_keys)
    {
      my $key = join("\0", sort $uk->columns);

      if($key eq $local_key)
      {
        $local_unique = 1;
        last;
      }
    }

    foreach my $uk ($f_meta->primary_key, $f_meta->unique_keys)
    {
      my $key = join("\0", sort $uk->columns);

      if($key eq $remote_key)
      {
        $remote_unique = 1;
        last;
      }
    }

    unless($local_unique && $remote_unique)
    {
      next FK;
    }


    # This is really a one-to-one fk/relationship
    $fk->relationship_type('one to one');

    # Find the associated relationship and change its type
    foreach my $rel ($self->relationships)
    {
      next  unless($rel->can('foreign_key'));
      my $rel_fk = $rel->foreign_key or next;

      if($rel_fk eq $fk) # string match on stringified object 
      {
        my $new_rel = 
          $self->_build_relationship(name => $rel->name,
                                     type => 'one to one',
                                     info => 
                                     {
                                       class      => $rel->class,
                                       column_map => scalar $rel->column_map,
                                     });

        $new_rel->foreign_key($fk);

        $self->relationship($rel->name => $new_rel);
      }
    }

    my $cm = $f_meta->convention_manager;

    # Also don't add one to one relationships between a class
    # and one of its map classes
    if($cm->is_map_class($class) && !$args{'include_map_class_relationships'})
    {
      $Debug && warn "$f_class - Refusing to make one to one relationship ",
                     "to map class to $class\n";
      next FK;
    }

    my $name = $cm->auto_relationship_name_one_to_one($self->table, $class);

    my $relationship = 
      $f_meta->_build_relationship(name => $name,
                                   type => 'one to one',
                                   info => 
                                   {
                                     class      => $class,
                                     column_map => { reverse %$key_cols },
                                   });

    # Skip if there's already a relationship with the same id
    foreach my $rel ($f_meta->relationships)
    {
      next FK  if($relationship->id eq $rel->id);
    }

    # Add the one to one relationship to the foreign class    
    unless($f_meta->relationship($name))
    {
      $Debug && warn "$f_class - Adding one to one relationship ",
                     "'$name' to $class\n";
      $f_meta->add_relationship($relationship);
    }

    # Create the methods, preserving existing methods
    $f_meta->make_relationship_methods(name => $name, preserve_existing => 1);
  }

  return;
}

sub auto_init_many_to_one_relationships { }

sub auto_init_one_to_many_relationships 
{
  my($self, %args) = @_;

  my $class = $self->class;

  # For each foreign key in this class, try to make a "one to many"
  # relationship in the table that the foreign key points to.  But
  # don't do so if there's already a one to one relationship in that 
  # class that references all of the foreign key's columns.
  FK: foreach my $fk ($self->foreign_keys)
  {
    my $f_class = $fk->class;

    next  unless($f_class && UNIVERSAL::isa($f_class, 'Rose::DB::Object'));

    my $f_meta  = $f_class->meta;
    my $key_cols = $fk->key_columns;

    # Check for any one to one relationships that reference the foreign
    # key's columns.  If found, don't try to make the one to many rel.
    REL: foreach my $rel ($f_meta->relationships)
    {
      if($rel->can('map_class'))
      {
        next  unless($rel->map_class eq $class);
      }
      else
      {
        next  unless($rel->class eq $class);
      }

      if($rel->type eq 'one to one' && !$rel->foreign_key)
      {
        my $skip = 1;

        my $col_map = $rel->column_map or next REL;

        foreach my $remote_col (values %$col_map)
        {
          $skip = 0  unless($key_cols->{$remote_col});
        }

        next FK  if($skip);
      }
    }

    # If both sides of the column map are unique or primary keys, then
    # this is really a one-to-one relationship
    my $local_key  = join("\0", sort keys %$key_cols);
    my $remote_key = join("\0", sort values %$key_cols);

    my($local_unique, $remote_unique);

    my $local_meta = $class->meta;

    foreach my $uk ($local_meta->primary_key, $local_meta->unique_keys)
    {
      my $key = join("\0", sort $uk->columns);

      if($key eq $local_key)
      {
        $local_unique = 1;
        last;
      }
    }

    foreach my $uk ($f_meta->primary_key, $f_meta->unique_keys)
    {
      my $key = join("\0", sort $uk->columns);

      if($key eq $remote_key)
      {
        $remote_unique = 1;
        last;
      }
    }

    if($local_unique && $remote_unique)
    {
      next FK;
    }

    my $cm = $f_meta->convention_manager;

    # Also don't add one to many relationships between a class
    # and one of its map classes
    if($cm->is_map_class($class) && !$args{'include_map_class_relationships'})
    {
      $Debug && warn "$f_class - Refusing to make one to many relationship ",
                     "to map class to $class\n";
      next FK;
    }

    my $name = $cm->auto_relationship_name_one_to_many($self->table, $class);

    my $relationship = 
      $f_meta->_build_relationship(name => $name,
                                   type => 'one to many',
                                   info => 
                                   {
                                     class      => $class,
                                     column_map => { reverse %$key_cols },
                                   });

    # Skip if there's already a relationship with the same id
    foreach my $rel ($f_meta->relationships)
    {
      next FK  if($relationship->id eq $rel->id);
    }

    # Add the one to many relationship to the foreign class    
    unless($f_meta->relationship($name))
    {
      $Debug && warn "$f_class - Adding one to many relationship ",
                     "'$name' to $class\n";
      $f_meta->add_relationship($relationship);
    }

    # Create the methods, preserving existing methods
    $f_meta->make_relationship_methods(name => $name, preserve_existing => 1);
  }

  return;
}

sub auto_init_many_to_many_relationships
{
  my($self, %args) = @_;

  my $class = $self->class;

  my $cm = $self->convention_manager;

  # Nevermind if this isn't a map class
  return  unless($cm->is_map_class($class));

  my @fks = $self->foreign_keys;

  # It's got to have just two foreign keys
  return  unless(@fks == 2);

  my $key_cols1 = $fks[0]->key_columns;
  my $key_cols2 = $fks[1]->key_columns;

  # Each foreign key must have key columns
  return  unless($key_cols1 && keys %$key_cols1 &&
                 $key_cols2 && keys %$key_cols2);

  my $map_class = $class;

  # Make many to many relationships in both foreign classes that go
  # through this map table
  PAIR: foreach my $pair ([ @fks ], [ reverse @fks ])
  {
    my($fk1, $fk2) = @$pair;

    my $class1 = $fk1->class;
    my $class2 = $fk2->class;

    my $meta = $class1->meta;
    my $name = $cm->auto_relationship_name_many_to_many($fk2, $map_class);

    my $relationship = 
      $meta->_build_relationship(name => $name,
                                 type => 'many to many',
                                 info => 
                                 {
                                   map_class => $map_class,
                                   map_from  => $fk1->name,
                                   map_to    => $fk2->name,
                                 });

    # Skip if there's already a relationship with the same id
    foreach my $rel ($meta->relationships)
    {
      next PAIR  if($relationship->id eq $rel->id);
    }

    unless($meta->relationship($name))
    {
      $Debug && warn "$class1 - Adding many to many relationship '$name' ",
                     "through $map_class to $class2\n";
      $meta->add_relationship($relationship);
    }

    # Create the methods, preserving existing methods
    $meta->make_relationship_methods(name => $name, preserve_existing => 1);
  }

  return;
}

sub auto_init_metadata_attributes
{
  my($self, %args) = @_;

  foreach my $column ($self->columns)
  {
    no warnings 'uninitialized';
    if($column->default =~ /^\w+\(.*\)$/)
    {
      $self->allow_inline_column_values(1);
      last;
    }
  }
}

sub auto_initialize
{
  my($self) = shift;
  my(%args) = @_;

  $self->auto_init_args({ %args });

  $self->allow_auto_initialization(1);
  $self->is_auto_initializating(1);

  $self->auto_init_columns(@_);
  $self->auto_init_primary_key_columns;
  $self->auto_init_unique_keys(@_);
  $self->auto_init_foreign_keys(@_);
  $self->auto_init_relationships(@_);
  $self->auto_init_metadata_attributes(@_);

  $self->initialize(@_);

  # Don't seem to need this anymore...
  #unless($args{'passive'})
  #{
  #  for(1 .. 2) # two passes are required to catch everything
  #  {
  #    $self->retry_deferred_foreign_keys;
  #    $self->retry_deferred_relationships;
  #    $self->retry_deferred_tasks;
  #  }
  #}

  unless($args{'stay_connected'})
  {
    my $meta_class = ref $self;
    $meta_class->clear_all_dbs;
  }

  $self->is_auto_initializating(0);
  $self->was_auto_initialized(1);

  return;
}

1;

__END__

KNOWN BUGS:

MySQL:

CHAR(6) column shows up as VARCHAR(6)
BIT(5)  column shows up as TINYINT(1) (MySQL 5.0.2 or earlier) 
BOOLEAN column shows up as TINYINT(1)
No native support for array types in MySQL