Select to view content in your preferred language

AppendManagement failing because of field widths

850
7
03-23-2022 08:38 AM
by Anonymous User
Not applicable

I have a script running that is updating an SDE feature class from a parcel fabric feature class..   

The script is failing because two fields in the target database are not wide enough.

Field 1 - 50 in the source , 20 in the target

Field 2 - 2000 in the source 25 in the target

I've tested and verified that appendmanagement will run fine if I alter the field widths in the target database.

I'd rather not do that and was wondering if there is a way to have appendmanagment just truncate the values at 20 and 25 when it does the append.

Feedback is greatly appreciated!

0 Kudos
7 Replies
DonMorrison1
Frequent Contributor

Did you try schema_type="NO_TEST" and the arcpy.Append_management call?  This allows appends when schemas don't match, however I suspect it will still fail if you have a value in one of those two fields that does not fit in the target column. But it might be worth a try.

0 Kudos
by Anonymous User
Not applicable

Thanks for that suggestion. In a test database I altered the widths of the fields to match the source database. It's not ideal but i figured I could at least test the results.

Unfortunately the field calc part of the script is failing now.

The fields as exported from the parcel feature class ended up being called 

SHAPE.STArea() and SHAPE.STLength()

I can't seem to get the correct syntax to add two new fields (AREA and PERMIETER) and calculate the new fields to these fields before deleting them.

When I call AddField it's naming the field Database_SCHEMA_featureclass.AREA instead of just AREA.

Here are the lines:
arcpy.AddField_management(feature_lyr_out , "AREA", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(feature_lyr_out , "AREA", "[!SHAPE.STArea()!]", "PYTHON_9.3", "")
arcpy.DeleteField_management(feature_lyr_out , "![SHAPE.STArea()]!")

I'm wondering if the system is having trouble resolving the field names that include . and () characters.

Feedback is appreciated.

 

0 Kudos
by Anonymous User
Not applicable

These are the fields: 

test.JPG

0 Kudos
DonMorrison1
Frequent Contributor

Try using "!shape.area!" in your expression instead of "![SHAPE.STArea()]!".

I'm not sure why it is prepending the field with the schema name but perhaps "area" is a reserved name and you should try something else like "my_area".

0 Kudos
by Anonymous User
Not applicable

It's failing because "area" is a reserved keyword in an SDE table. You'll have to name it "my_area" (or something) to add that field without the inserted qualifications

0 Kudos
by Anonymous User
Not applicable

I believe the NO_TEST will still fail at the DB level.

0 Kudos
by Anonymous User
Not applicable

To answer your original question - One way around this is something like this...

 

 

fld1 = "your field that's too long"
fld2 = "another"
desc = arcpy.Describe(table)
for fld_obj in desc.fields()
    if fld.name == fld1:
        fld1_len = fld.length
    if fld.name == fld2:
        fld2_len = fld.length
with arcpy.da.UpdateCursor("your table", ["fld1", "fld2"]) as uCur:
    for uRow in uCur:
        uRow[0] = value_too_long[:fld1_len]
        uRow[1] = value_too_long[:fld2_len]
        uCur.updateRow(uRow)

 

 

 

the value_too_long[:fld2_len] is a string slice that goes up to the length identified in the first part. (it may need a "fld2_len - 1" if it's still too long.

disclaimer: this is off the top of my head which is more 2.7 based. Also, getting the value_too_long value is hard for me to guess since I don't know where it's coming from. if it's another field inthe same table, you would add it to the list of fields in the UpdateCursor initialization

 

edit: since you already know their lengths, you could skip the first section and just put it in the value_too_long

for fld_obj in desc.fields()

 

0 Kudos